`
OliveExcel
  • 浏览: 70410 次
文章分类
社区版块
存档分类
最新评论

Android 3.1后, 新安装的以及用户强行停止的apk, 无法监听广播的问题

阅读更多

         写了这么一个程序: 代码中只包含一个BroadcastReceiver, 监听开机广播, 然后静态注册在AndroidManifest.

         碰到这么一个问题: 上述Receiver死活不触发.

         找到这么一个解释软件必须要运行一次才能接收到开机广播的,这是谷歌在android高版本推出的安全机制

         官方是这么说的(详见http://developer.android.com/about/versions/android-3.1.html):

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.

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).

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.

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.

       总的来说, 就是自Android 3.1之后, 所有新安装但未被执行过的apk, 以及那些被用户强行停止的apk, 都会处于stopped状态

       这个状态下, apk中的广播接收器, 均处于未激活状态, 无法履行监听功能.

 

         解决方案:

         引入程序对其他apk的依赖, 即先依赖其他程序发送广播, 广播Intent中包含FLAG_INCLUDE_STOPPED_PACKAGES, 以激活该Receiver. 在不改变开机广播FLAG的前提下, apk实现"不经执行就监听开机广播"的功能, 暂时无解.

         如果读者有较好的解决方案, 还望不吝赐教!

 

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics