请自行参阅博文:
http://blog.csdn.net/loovejava/article/details/9261907
android3.1以后,packageManager增加了对“stop state”的应用的管理,这个stopped和Activity生命周期中的stop状态是完全两码事,指的是安装后从来没有启动过和被用户手动强制停止的应用,与此同时系统增加了2个Flag:FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES ,来标识一个intent是否激活处于“stopped state”的应用。当2个Flag都不设置或者都进行设置的时候,采用的是FLAG_INCLUDE_STOPPED_PACKAGES的效果。
google给所有的广播intent默认加上FLAG_EXCLUDE_STOPPED_PACKAGES效果卓著,能在一定程度上增加安全系数,还能提高效率,但是RECEIVE_BOOT_COMPLETED广播如果用户没有运行过应用,就不会响应了。
解决这个问题还有一种途径,google允许应用和后台服务通过给广播intent设置FLAG_INCLUDE_STOPPED_PACKAGES来唤醒处于“stopped state”的程序,也就是用户自己写的广播intent可以控制这个机制,但是系统自带的广播intent,由于不能修改,就只能接受这个现实了。
补充:
一般应用安装完后, 若用户不手动启动一次, 则会在用户在文件中保存此应用的信息:
/data/system/users/0/package-restrictions.xml
文件内容见:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <package-restrictions> <pkg name="com.google.android.ears" enabled="2" /> <pkg name="com.android.contacts"> <disabled-components> <item name="com.android.contacts.activities.DialtactsActivity" /> </disabled-components> </pkg> <pkg name="com.boohee.one"> <enabled-components> <item name="com.xiaomi.push.service.XMPushService" /> </enabled-components> </pkg> <pkg name="com.google.android.partnersetup"> <enabled-components> <item name="com.google.android.partnersetup.PhoneStateReceiver" /> </enabled-components> </pkg> <pkg name="com.anson.boot" stopped="true" nl="true" />
最后一行中stoped="true", nl="true"
nl 是 notlaunched的意思.
此文件的读写见文件:
./frameworks/base/services/java/com/android/server/pm/Settings.java
在./frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
中声明了方法代外部调用:
public void setPackageStoppedState(String packageName, boolean stopped, int userId) { if (!sUserManager.exists(userId)) return; final int uid = Binder.getCallingUid(); final int permission = mContext.checkCallingOrSelfPermission( android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE); final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED); enforceCrossUserPermission(uid, userId, true, "stop package"); // writer synchronized (mPackages) { if (mSettings.setPackageStoppedStateLPw(packageName, stopped, allowedByPermission, uid, userId)) { scheduleWritePackageRestrictionsLocked(userId); } } }
如:
./frameworks/base/services/java/com/android/server/am/ActivityStack.java
final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options){ ... // Launching this app's activity, make sure the app is no longer // considered stopped. try { AppGlobals.getPackageManager().setPackageStoppedState( next.packageName, false, next.userId); /* TODO: Verify if correct userid */ } catch (RemoteException e1) { } catch (IllegalArgumentException e) { Slog.w(TAG, "Failed trying to unstop package " + next.packageName + ": " + e); }