展会信息港展会大全

Android对开机自启动的限制 安卓开发
来源:互联网   发布日期:2016-01-19 12:19:08   浏览:3236次  

导读:谈到如何实现程序的开机自启动, 我想大多数朋友都会认为小菜一碟,不就是注册一个BOOT_COMPLETED消息嘛!在AndroidManifest xml里面加入一个receiver,就像这样:SPAN style=FONT-SIZE: 18pxreceiver ...

谈到如何实现程序的开机自启动, 我想大多数朋友都会认为小菜一碟,不就是注册一个BOOT_COMPLETED消息嘛!

在AndroidManifest.xml里面加入一个receiver,就像这样:

<SPAN style="FONT-SIZE: 18px"> <receiver android:name="BootReceiver">

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED"/>

<action android:name="com.android.settings.MAIL_SENT"/>

</intent-filter>

</receiver></SPAN>

但是很不幸,这个只在android 2.3及更老的版本中有效,在新版本的android系统中,这种方法是 无法保证开机自动启的。

首先看看google的官方文档:

<SPAN style="FONT-SIZE: 18px">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_PACKAGESInclude intent filters of stopped applications in the list of potential targets to resolve against.

FLAG_EXCLUDE_STOPPED_PACKAGESExclude 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).</SPAN>

上面的文档主要讲了一下几点:

1、android 3.1中有一类package 叫做stopped package, 它们就是那种安装了但是从来没有启动过的apk,或者被用户在程序管理里面force stop了的apk

2、intent中新加了一组flag(FLAG_INCLUDE_STOPPED_PACKAGES和 FLAG_EXCLUDE_STOPPED_PACKAGES),带有FLAG_INCLUDE_STOPPED_PACKAGES的 intent对stopped package是不起作用的。

3、系统对所有的广播intent都加了 flag:FLAG_EXCLUDE_STOPPED_PACKAGES,当然boot complete广播也不例外。

看完这3点,大家应该知道为什么我在前面说:注册静态receiver无法保证app开机自动启。

原因很简单,如果我的apk安装到手机后,一直都没有被用户启动过(或者被force stop过),那么它就是一个stopped package,boot_complete广播是无法将其拉起的。

这里需要注意一点: /system/app下面的apk都是非 stopped package。所以如果你的手机root了,那就另当别论了,你可以把apk push到system/app,但是这个安装方式不是正常安装方式了。

所以朋友们如果碰到apk无法开机自动启,请不必苦恼,先看看你的android 版本吧。

android为什么要加入这一机制呢?

很明显是出于安全考虑,因为开机自启动是病毒程序的惯用方式,这个机制可以在一定程度上防止病毒程序的开机自启 动。

赞助本站

人工智能实验室

相关热词: 开机 自启动 Android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港