Android 3.1增加FLAG_EXCLUDE_STOPPED_PACKAGES和FLAG_INCLUDE_STOPPED_PACKAGES机制

Android3.1增加FLAG_EXCLUDE_STOPPED_PACKAGESFLAG_INCLUDE_STOPPED_PACKAGES机制

Android3.1之后,googleAndroid里面为广播增加了两个flagFLAG_EXCLUDE_STOPPED_PACKAGESFLAG_INCLUDE_STOPPED_PACKAGES

/**
 * If set, this intent will not match anycomponents in packages that
 * are currently stopped.  If this is not set, then the default behavior
 * is to include such applications in theresult.
 */
public static final int FLAG_EXCLUDE_STOPPED_PACKAGES = 0x00000010;
/**
 * If set, this intent will always matchany components in packages that
 * are currently stopped.  This is the default behavior when
 * {@link #FLAG_EXCLUDE_STOPPED_PACKAGES}is not set.  If both of these
 * flags are set, this one wins (itallows overriding of exclude for
 * places where the framework mayautomatically set the exclude flag).
 */
public static final int FLAG_INCLUDE_STOPPED_PACKAGES = 0x00000020;

FLAG_EXCLUDE_STOPPED_PACKAGES表示此广播将不会被stopped状态的应用接收到,系统广播默认会被加上此flag,这就是为什么有时候开机广播没有被appreceiver收到,很可能的一个原因就是你的app处于stopped状态,至于app什么时候会被设置成stopped状态,稍后再讲。补充,系统应用并不受stopped状态的影响,会正常接收到此flag的广播,有代码为证:

@Override
protected boolean isFilterStopped(PackageParser.ActivityIntentInfofilter, int userId) {
   
if (!sUserManager.exists(userId))return true;
   
PackageParser.Package p = filter.activity.owner;
    if
(p != null) {
        PackageSetting ps = (PackageSetting)p.mExtras
;
        if
(ps != null) {
           
// System apps are never considered stopped for purposesof
            // filtering, because theremay be no way for the user to
            // actually re-launch them.
           
return (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM)== 0
                   
&& ps.getStopped(userId);
       
}
    }
   
return false;
}

上面是PackageManagerServiceisFilterStopped方法,该方法会在系统判断应用是否能接受广播的时候用到,可以看出,系统应用远远不会被当成stopped状态来处理,并且不需要用户手动打开一次

FLAG_INCLUDE_STOPPED_PACKAGES表示此广播将会被处于stopped状态的应用接收到。

应用什么时候被设置成stopped状态?

当应用首次安装并且没有被打开过或者应用被强制停止的时候,应用会处于stopped状态。应用被强制停止会调用ActivityManagerServiceforceStopPackage方法,感兴趣的朋友可以跟下去看看stopped状态是如何设置的,关于该流程的类还有PackageSetting、PackageSettingBase、PackageManagerService、PackageuserState等。网上有人说被强制停止的应用会在/data/system/packages-stopped.xml保存下来,这个文件是在Settings.java(frameworks\base\services\core\java\com\android\server\pm)中创建的。

Settings(File dataDir, Object lock) {
    mLock = lock;

    mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock);

    mSystemDir = new File(dataDir, "system");
    mSystemDir.mkdirs();
    FileUtils.setPermissions(mSystemDir.toString(),
            FileUtils.S_IRWXU|FileUtils.S_IRWXG
                    |FileUtils.S_IROTH|FileUtils.S_IXOTH,
            -1, -1);
    mSettingsFilename = new File(mSystemDir, "packages.xml");
    mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
    mPackageListFilename = new File(mSystemDir, "packages.list");
    FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID);

    final File kernelDir = new File("/config/sdcardfs");
    mKernelMappingFilename = kernelDir.exists() ? kernelDir : null;

    // Deprecated: Needed for migration
    mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
    mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
}
但是本人在某些设备上面并没有看到packages-stopped.xml文件,如果在这个目录找不到package-stopped.xml文件,根据源码的注释,改文件可能被移动了,可以试试data/system/users/0/package-restrictions.xml,我在这个文件里面找到了package的stop状态,不同的设备厂商的目录可能不一样,仅供参考。

猜你喜欢

转载自blog.csdn.net/baidu_27196493/article/details/80434454