您的位置:首页 > 移动开发 > Android开发

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

2013-08-22 13:43 507 查看
对于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.

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.

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 the
FLAG_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)的应用程序。

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



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

至于如何让自己的应用收到开机启动广播,目前还没有找到好的方案
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: