`

Android开机自启动程序设置及控制方法思路浅谈

阅读更多

Android系统通过应用程序自行在系统中登记注册事件(即Intent)来响应系统产生的各类消息。

例如Android实现系统开机自启动程需要在Manifest中加入如下Intent-filter及权限Uses-permission即可。
  1.     <intent-filter>  
  2.         <action android:name="android.intent.action.BOOT_COMPLETED"/>  
  3. (修改时候主要是去掉上面该行即可)
  4.         <category android:name="android.intent.category.HOME" />  
  5.     </intent-filter>  
  6. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>   
  7. (修改时候主要是去掉上面该行即可)
  8. Android系统为应用程序管理功能提供了大量的API,可以通过API控制Intent和permission,其中

上述配置表示应用程序会响应系统产生的android.intent.action.BOOT_COMPLETED(系统启动完成)信号,以此来实现应用程序自启动。当然知道上述原理后,我们就可以随心所欲的控制程序开机自启动了。具体思路如下:

 

一、手工方法

基于上述原理,我们可以通过对系统中已安装的程序去除其Manifest的上述配置片段来控制应用程序的对系统的响应,当然没源码可修改编译的情况下只能实现屏蔽其对有些信号的响应,例如屏蔽该程序不再开机自启动。手工方法就是利用有关工具直接在解压其APK包后,修改其Manifest的上述配置行后再打包成APK,最后安装到系统中就实现了屏蔽其自启动功能。具体相关的工具软件主要有APKTOOL。(请自己放狗去搜索下载)

 

二、编程实现

 

当然手工方法需要借助APKTOOL等工具,步骤比较法繁琐,我们可以通过自己开发来实现该功能。幸好

1、PackageManager

本类API是对所有基于加载信息的数据结构的封装,包括以下功能:

•安装,卸载应用
•查询permission相关信息
•查询Application相关信息(application,activity,receiver,service,provider及相应属性等)
•查询已安装应用
•增加,删除permission
•清除用户数据、缓存,代码段等
非查询相关的API需要特定的权限,具体的API请参考SDK文档。

2、ActivityManager相关

本类API是对运行时管理功能和运行时数据结构的封装,包括以下功能

•激活/去激活activity
•注册/取消注册动态接受intent
•发送/取消发送intent
•activity生命周期管理(暂停,恢复,停止,销毁等)
•activity task管理(前台->后台,后台->前台,最近task查询,运行时task查询)
•激活/去激活service
•激活/去激活provider等
task管理相关API需要特定的权限,具体API可参考SDK文档。

 

利用上述API原理的具体代码俺有空时候试试实现一个,应该不难的,都是调用现成的API实现。目前发现已有的控制开机自启动的成熟应用程序主要是autostarts,可自己安装一个试试,挺好用,实际上autostart有点名不副实,如果是我就会起个名字叫Intentcontrol,因为其不只是控制开机启动信号,它可以控制程序对大部分信号的响应行为。

 

三、系统自带工具

Android自带了1、程序包管理工具:/system/bin/pm  2、activity管理工具:/system/bin/am

具体用法大致说明如下:

pm的使用方法可以参考 

usage: pm [list|path|install|uninstall]
       pm list packages [-f]
       pm list permission-groups
       pm list permissions [-g] [-f] [-d] [-u] [GROUP]
       pm list instrumentation [-f] [TARGET-PACKAGE]
       pm list features
       pm path PACKAGE
       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] PATH
       pm uninstall [-k] PACKAGE
       pm enable PACKAGE_OR_COMPONENT
       pm disable PACKAGE_OR_COMPONENT

The list packages command prints all packages.  Options:
  -f: see their associated file.

The list permission-groups command prints all known
permission groups.

The list permissions command prints all known
permissions, optionally only those in GROUP.  Options:
  -g: organize by group.
  -f: print all information.
  -s: short summary.
  -d: only list dangerous permissions.
  -u: list only the permissions users will see.

The list instrumentation command prints all instrumentations,
or only those that target a specified package.  Options:
  -f: see their associated file.

The list features command prints all features of the system.

The path command prints the path to the .apk of a package.

The install command installs a package to the system.  Options:
  -l: install the package with FORWARD_LOCK.
  -r: reinstall an exisiting app, keeping its data.
  -t: allow test .apks to be installed.
  -i: specify the installer package name.

The uninstall command removes a package from the system. Options:
  -k: keep the data and cache directories around.
after the package removal.

The enable and disable commands change the enabled state of
a given package or component (written as "package/class").

 

am的使用方法可以参考 

usage: am [subcommand] [options]

    start an Activity: am start [-D] <INTENT>
        -D: enable debugging

    send a broadcast Intent: am broadcast <INTENT>

    start an Instrumentation: am instrument [flags] <COMPONENT>
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
        -p <FILE>: write profiling data to <FILE>
        -w: wait for instrumentation to finish before returning

    start profiling: am profile <PROCESS> start <FILE>
    stop profiling: am profile <PROCESS> stop

    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>] [<URI>]

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics