-
Notifications
You must be signed in to change notification settings - Fork 0
android.intent.action.BOOT_COMPLETED
panwj edited this page Jul 20, 2017
·
1 revision
AndroidManifest.xml中注册BOOT_COMPLETED Action 注意不仅要添加android.intent.action.BOOT_COMPLETED对应的action,还需要添加对应的uses-permission
public class BootBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = "BootBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction().toString();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
// u can start your service here
Toast.makeText(context, "boot completed action has got", Toast.LENGTH_LONG).show();
return;
}
}
}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状态。
我们可以通过 adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
命令发送BOOT_COMPLETED广播,而不用重启测试机或模拟器来测试BOOT_COMPLETED广播,这条命令可以更精确的发送到某个package,如下:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n package_name/class_name