Android 无法接收开机广播的问题

对于Android的低版本接受不到开机广播主要有以下几个问题:


一.没有给应用添加权限 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

这个不是主要原因,因为经测试,即便是不加这个权限也可以收到系统广播

二.应用安装到了SD卡中,这种情况出现的机会也不多

三.(个别情况)手机或模拟器的开机模式为(fast boot)快速开机模式,也不能收到系统开机广播(据说只有个别的HTC手机才有此选项)

四.忘记写<intent-filter>

        <receiver
            android:name="com.darren.broadcastreceiver.BootBroadcastReceiver" >
            <intent-filter>
                <!-- 开机启动广播动作名称 -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

所以说,低版本的android收不到开机启动广播的可能性根本不大。


对于高版本的android应用收不到系统开机广播为正常现象,原因如下:


经过分析发现,如果应用程序安装上始终没有被打开过,那么在Android启动时,该应用无法接收到开机启动广播android.permission.RECEIVE_BOOT_COMPLETED。

原来在Android 3.1的更新文档中已经做了说明。

下面是引自Android官方API说明,地址 http://developer.android.com/about/versions/android-3.1.html#launchcontrols

原文如下

Launch controls on stopped applications

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.

When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

翻译如下

从Android 3.1开始,系统的软件包管理器跟踪处于停止状态(stopped state)的应用程序,提供了一种控制其启动后台进程和其他应用程序方式。

需要注意的是应用程序的停止状态(stopped state)和Activity的停止状态是不一样的。该系统可以分别管理这两种停止状态。

该平台定义了两个新的Intent的Flag,让发送者指定的意图是否应该被允许激活停止的应用程序的组件。

FLAG_INCLUDE_STOPPED_PACKAGES - Include intent filters of stopped applications in the list of potential targets to resolve against.包括停止的应用程序列表中的。
FLAG_EXCLUDE_STOPPED_PACKAGES - Exclude intent filters of stopped applications from the list of potential targets.排除停止的应用程序列表中的。

当两个Flag都不设置或都设置的时候,默认操作是FLAG_INCLUDE_STOPPED_PACKAGES。

请注意,系统向所有的Intent的广播添加了FL​​AG_EXCLUDE_STOPPED_PACKAGES标志。它这样做是为了防止广播无意中的或不必要地开启组件的stoppped应用程序的后台服务。后台服务或应用程序可以通过向广播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES标志来唤醒处于停止状态(stopped state)的应用程序。


应用程序处于停止状态情况有两种,一种是他们是第一次安装,但尚未启动,另一种是在管理应用程序中由用户手动停止。

 

简单的说,就是防止开机启动恶意程序,优化启动。经过验证发现,系统级的应用程序是可以接收到开机启动广播的。

至于如何让自己的应用收到开机启动广播,目前还没有找到好的方案


Android系统中,广播接收器(BroadcastReceiver)是一种用于接收来自系统或应用发出的广播通知的组件。当设备开机后,系统会发送一个特定的广播(Intent),任何注册了监听该广播广播接收器都可以接收到这个开机信号。应用可以通过在AndroidManifest.xml文件中声明或在代码中动态注册的方式来监听开机广播。 以下是一个简单的示例,展示如何在AndroidManifest.xml中声明一个广播接收器来监听开机信号: ```xml <receiver android:name=".BootCompletedReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> ``` 在上述代码中,`.BootCompletedReceiver`是一个自定义的广播接收器类。你需要在该类中重写`onReceive`方法,以便在开机完成后执行相应的逻辑: ```java public class BootCompletedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 在这里实现开机后需要执行的操作 } } ``` 在代码中动态注册广播接收器的方式如下: ```java IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); BootCompletedReceiver receiver = new BootCompletedReceiver(); registerReceiver(receiver, filter); ``` 这种方式不需要在AndroidManifest.xml中声明广播接收器,但需要在应用运行时执行上述代码来注册接收器。 请注意,在Android 8.0(API 级别 26)及以上版本,对于后台应用接收广播有了更多的限制,因此如果你的应用需要在这些版本上接收开机广播,可能需要使用JobScheduler、WorkManager或者设置组件(如NotificationListenerService)来接收
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值