Android应用如何开机自启动、自启动失败缘由

Android应用如何开机自启动、自启动失败缘由

本文主要介绍Android应用如何开机自启动、自启动失败的缘由、adb命令发送BOOT_COMPLETED
问题:应用程序是否能够在安装后自启动,没有ui的纯service应用如何启动?答案立刻揭晓^_*
一、Android应用如何开机自启动
(1)、在AndroidManifest.xml中注册html

AndroidManifest.xml中注册BOOT_COMPLETED Action android

注意不只要添加android.intent.action.BOOT_COMPLETED对应的action,还须要添加对应的uses-permissionshell

 

(2)、Receiver接收广播进行处理安全

Javaapp

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;        }    } }ide

1测试

2ui

3spa

4xml

5

6

7

8

9

10

11

12

13

14

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;

        }

    }

}

 

二、自启动失败的缘由
接收不到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状态,在这种状态下接收不到任何广播。直到被启动过(用户打开或是其余应用调用)才会脱离这种状态,因此Android3.1以后
(1)、应用程序没法在安装后本身启动
(2)、没有ui的程序必须经过其余应用激活才能启动,如它的Activity、Service、Content Provider被其余应用调用。
存在一种例外,就是应用程序被adb push you.apk /system/app/下是会自动启动的,不处于stopped状态。no broadcast received stopped state
具体说明见:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html

 

三、adb发送BOOT_COMPLETED
咱们能够经过

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

1

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

1

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n package_name/class_name

相关文章
相关标签/搜索