一、应用开机启动
1、添加权限:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2、广播接收器静态注册且增加相应的action
<receiver android:name=".BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
3、在receiver中启动应用需添加标识
Intent bootStartIntent = new Intent(context,MainActivity.class);
bootStartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(bootStartIntent);
二、使用adb发送BOOT_COMPLETED
我们可以通过
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
命令发送BOOT_COMPLETED广播,而不用重启测试机或模拟器来测试BOOT_COMPLETED广播,这条命令可以更精确的发送到某个package,如下:
adb shell am broadcast -aandroid.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -npackage_name/class_name
三、几种不同方式安装应用的测试
a) 使用U盘安装签名的普通应用
结论:此开机启动是成功的,但应用需要运行一次程序,否则收不到开机广播!
b) 使用IDE运行应用到设备上
结论:此开机启动是成功的
c) 应用放到system/app目录中成为系统级应用
结论:此开机启动是成功的,无需应用启动过一次。
四、自启动失败的原因
接收不到BOOT_COMPLETED广播可能的原因
(1)、BOOT_COMPLETED对应的action和uses-permission没有一起添加
(2)、应用安装到了sd卡内,安装在sd卡内的应用是收不到BOOT_COMPLETED广播的
(3)、系统开启了Fast Boot模式,这种模式下系统启动并不会发送BOOT_COMPLETED广播
(4)、应用程序安装后重来没有启动过,这种情况下应用程序接收不到任何广播,包括BOOT_COMPLETED、ACTION_PACKAGE_ADDED、CONNECTIVITY_ACTION等等。
Android3.1之后,系统为了加强了安全性控制,应用程序安装后或是(设置)应用管理中被强制关闭后处于stopped状态,在这种状态下接收不到任何广播,除非广播带有FLAG_INCLUDE_STOPPED_PACKAGES标志,而默认所有系统广播都是FLAG_EXCLUDE_STOPPED_PACKAGES的,所以就没法通过系统广播自启动了。所以Android3.1之后
(1)、应用程序无法在安装后自己启动
(2)、没有ui的程序必须通过其他应用激活才能启动,如它的Activity、Service、Content Provider被其他应用调用。
存在一种例外,就是应用程序被adb push you.apk/system/app/下是会自动启动的,不处于stopped状态。
具体详见:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
五、相关文档翻译
Launchcontrols on stopped applications
有关停止的应用的启动控制
Startingfrom Android 3.1, the system's package manager keeps track of applications thatare in a stopped state and provides a means of controlling their launch frombackground processes and other applications.
从Android3.1开始,系统包管理器保存停止状态应用的痕迹,并提供一种方式控制它们从后台进程和其他应用启动。
Notethat an application's stopped state is not the same as an Activity's stoppedstate. The system manages those two stopped states separately.
注意应用的停止状态和活动的停止状态是不同的。系统分别管理这两种停止状态。
Theplatform defines two new intent flags that let a sender specify whether theIntent should be allowed to activate components in stopped application.
平台定义了两个新的意图标识,让发送方指定意图是否允许激活停止的应用中的组件。
FLAG_INCLUDE_STOPPED_PACKAGES— Include intent filters of stopped applications in the list of potentialtargets to resolve against.
FLAG_INCLUDE_STOPPED_PACKAGES-针对解决包含停止的应用的意图过滤器在潜在目标名单中。
FLAG_EXCLUDE_STOPPED_PACKAGES— Exclude intent filters of stopped applications from the list of potentialtargets.
FLAG_EXCLUDE_STOPPED_PACKAGES-排除停止的应用的意图过滤器在潜在目标名单中。
Whenneither or both of these flags is defined in an intent, the default behavior isto include filters of stopped applications in the list of potential targets.
当这些标志没有或两者都被定义在一个意图中,则默认行为是包括停止的应用的过滤器在潜在目标名单中。
Notethat the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. Itdoes this to prevent broadcasts from background services from inadvertently orunnecessarily launching components of stoppped applications. A backgroundservice or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowedto activate stopped applications.
需要注意的是该系统增加FLAG_EXCLUDE_STOPPED_PACKAGES到所有的广播意图。它这样做是为了防止来自后台服务的广播无意或不必要的启动停止的应用程序的组件。后台服务或应用程序可以通过添加FLAG_INCLUDE_STOPPED_PACKAGES标志覆盖此行为,以广播允许激活停止的应用程序的意图。
Applicationsare in a stopped state when they are first installed but are not yet launchedand when they are manually stopped by the user (in Manage Applications).
当应用程序第一次安装且没有被启动过或者手动被用户停止(在管理应用程序中)时,应用程序处于被停止状态。