关于BOOT_COMPLETED广播-自启动

文章摘要:
1、BOOT_COMPLETED在ActivityManagerService中由系统发送。
2、应用可以监听该广播,成为自启动权限,但是这样会有很多缺点,最大的缺点就是拖慢开机进度,影响用户体验。
3、开机状态会sys.boot_completed,可以通过该属性状态得到开机状态。


一、BOOT_COMPLETED广播是什么?
1、BOOT_COMPLETED是系统在开机加载完毕后发送的。如下:

/**
     * Broadcast Action: This is broadcast once, after the system has finished
     * booting.  It can be used to perform application-specific initialization,
     * such as installing alarms.  You must hold the
     * {@link android.Manifest.permission#RECEIVE_BOOT_COMPLETED} permission
     * in order to receive this broadcast.
     *
     * <p class="note">This is a protected intent that can only be sent
     * by the system.
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";

从注释可以看出,该广播需要权限才能接收,并且只有系统有权限发送它。
2、接收BOOT_COMPLETED广播,需要权限,该权限:android.Manifest.permission#RECEIVE_BOOT_COMPLETED。
如下是系统对该权限的介绍:

    <!-- Allows an application to receive the
         {@link android.content.Intent#ACTION_BOOT_COMPLETED} that is
         broadcast after the system finishes booting.  If you don't
         request this permission, you will not receive the broadcast at
         that time.  Though holding this permission does not have any
         security implications, it can have a negative impact on the
         user experience by increasing the amount of time it takes the
         system to start and allowing applications to have themselves
         running without the user being aware of them.  As such, you must
         explicitly declare your use of this facility to make that visible
         to the user. -->
    <permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"
        android:permissionGroup="android.permission-group.APP_INFO"
        android:protectionLevel="normal"
        android:label="@string/permlab_receiveBootCompleted"
        android:description="@string/permdesc_receiveBootCompleted" />
  • a、从ProtectionLevel来看,应用可以申请该权限。android:protectionLevel="normal"
  • b、如文中所述,申请该权限,有许多缺点。
    • I、增加系统启动的时间。因为系统启动过程中,广播接收方会收到广播,并可以启动自身或者加载某些资源。
    • II、自启动,不经用户确认。应用都说在用户的点击或者请求发起下启动的,自启动可以绕过用户,自我自动。
    • III、不要想着自己的小行为用户觉察不到,系统会将自启动的应用程序形成一份列表名单,show给用户,并交由用户管理。
隐私授权

二、如何接收该广播。
1、前面提到了,接收BOOT_COMPLETED广播,需要权限,该权限:android.Manifest.permission#RECEIVE_BOOT_COMPLETED。
在AndroidManifest.xml中增加权限信息:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

2、增加广播注册。
在AndrodManifest.xml中,增加广播注册信息:

<receiver android:name="com.example.androidtest.BootCompleteReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

三、BOOT_COMPLETED系统如何发送的。
1、在系统启动的过程中,ActivityManagerServie发送的该广播。
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java#finishBooting

if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
    // Start looking for apps that are abusing wake locks.
    Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
    mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
    // Tell anyone interested that we are done booting!
    SystemProperties.set("sys.boot_completed", "1");

    // And trigger dev.bootcomplete if we are not showing encryption progress
    if (!"trigger_restart_min_framework".equals(SystemProperties.get("vold.decrypt"))
        || "".equals(SystemProperties.get("vold.encrypt_progress"))) {
        SystemProperties.set("dev.bootcomplete", "1");
    }
    for (int i=0; i<mStartedUsers.size(); i++) {
        UserStartedState uss = mStartedUsers.valueAt(i);
        if (uss.mState == UserStartedState.STATE_BOOTING) {
            uss.mState = UserStartedState.STATE_RUNNING;
            final int userId = mStartedUsers.keyAt(i);
            Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED, null);
            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
            intent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT);
            broadcastIntentLocked(null, null, intent, null,
                    new IIntentReceiver.Stub() {
                        @Override
                        public void performReceive(Intent intent, int resultCode,
                                String data, Bundle extras, boolean ordered,
                                boolean sticky, int sendingUser) {
                            synchronized (ActivityManagerService.this) {
                                requestPssAllProcsLocked(SystemClock.uptimeMillis(),
                                        true, false);
                            }
                        }
                    },
                    0, null, null,
                    android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
                    AppOpsManager.OP_NONE, true, false, MY_PID, Process.SYSTEM_UID,
                    userId);
        }
    }
    scheduleStartProfilesLocked();
}

2、从上面粘贴的代码中,我们可以得出如下结论。

  • a、开机启动广播,是区分多用户的。
  • b、在开机广播发送的流程中,会向系统写入sys.boot_completed,我们依据它可以得出当前系统是否已开机加载结束,这对一些系统服务或者app判断应用状态很有效。
SystemProperties.set("sys.boot_completed", "1");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容