谈谈Android中的persistent属性

在我们开发系统级的App时,很有可能就会用persistent属性。当在AndroidManifest.xml中将persistent属性设置为true时,那么该App就会具有如下两个特性:

在系统刚起来的时候,该App也会被启动起来

该App被强制杀掉后,系统会重启该App。这种情况只针对系统内置的App,第三方安装的App不会被重启。

1. persistent属性的定义
persistent属性定义在frameworks/base/core/res/res/values/attrs_manifest.xml中:

<!-- Flag to control special persistent mode of an application.  This should
     not normally be used by applications; it requires that the system keep
     your application running at all times. -->
<attr name="persistent" format="boolean" />

官方解释:

是一个用于控制应用程序特殊持久模式的标志。通常情况下不应被应用程序使用,它要求系统始终保持应用程序的运行。

2. persistent属性的使用

persistent用在AndroidManifest.xml的application标签上:

<application
    android:persistent="true|false">

</application>

默认值为false。

3. persistent属性的原理分析
下面我们就从源码的角度来分析persistent属性的工作原理。

备注:本文的源码是Android6.0,不同的Android版本可能略有所不同。

3.1 persistent属性的解析
属性的解析主要发生在App安装或者系统启动的时候,解析代码的位置在:

/frameworks/base/core/java/android/content/pm/PackageParser.java

深入到PackageParser.java的parseBaseApplication中:

   private boolean parseBaseApplication(Package owner, Resources res,
            XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
        throws XmlPullParserException, IOException {

	final ApplicationInfo ai = owner.applicationInfo;
    final String pkgName = owner.applicationInfo.packageName;

    TypedArray sa = res.obtainAttributes(attrs,
            com.android.internal.R.styleable.AndroidManifestApplication);

	...省略...

		if ((flags&PARSE_IS_SYSTEM) != 0) {
	            if (sa.getBoolean(
	                    com.android.internal.R.styleable.AndroidManifestApplication_persistent,
	                    false)) {
	                ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
	            }
	        }

	...省略...
}

在解析完系统中App的包信息后,会将解析好的信息保存在PMS中的mPackages的map中,ApplicationInfo的flag中有一个bit位用于保存该App是否是persistent。

这里主要是将persistent的flag设置为ApplicationInfo.FLAG_PERSISTENT。

3.2 系统启动persistent为true的App
在系统启动时,会启动persistent属性为true的App,代码位置在:

/frameworks/base//services/core/java/com/android/server/am/ActivityManagerService.java

在系统启动时,AMS中的systemReady()方法会将所有在AndroidManifest中设置了persistent为true的App进程拉起来。

深入到AMS的systemReady()方法中:

public void systemReady(final Runnable goingCallback) {

	...省略...

    synchronized (this) {
        if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
            try {
                List apps = AppGlobals.getPackageManager().
                    getPersistentApplications(STOCK_PM_FLAGS);//注释1
                if (apps != null) {
                    int N = apps.size();
                    int i;
                    for (i=0; i<N; i++) {
                        ApplicationInfo info
                            = (ApplicationInfo)apps.get(i);
                        if (info != null &&
                                !info.packageName.equals("android")) {
                            addAppLocked(info, false, null /* ABI override */);//注释2
                        }
                    }
                }
            } catch (RemoteException ex) {
                // pm is in same process, this will never happen.
            }
        }
		...省略...
	}

	...省略...

注释说明:

注释1:调用PackageManagerServices的getPersistentApplications方法获取所有在AndroidManifest中设置了persistent属性为true的App

注释2:调用ActivityManagerServcies的addAppLocked方法去启动App


————————————————
版权声明:本文为优快云博主「salmon_zhang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/salmon_zhang/article/details/90741912

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值