AndroidManifest中android:persistent属性研究
来源:互联网 发布:js设置ireadonly input 编辑:程序博客网 时间:2024/06/08 14:28
平台:android4.0
场景:处理一个项目的时候,发现客户内置的一个music相关的apk每次都开机自动启动,同时在eclipse里面如何stop或者是调用killAllBackgroundProcesses()方法都无法停止此apk。
时间:2013.3
反编译apk,发现其AndroidManifest.xml文件中有一个类似如下描述:
<application android:name="PhoneApp" android:persistent="true"
在AMS::systemReady()函数中,有启动persistent的标签的app的代码:
List apps = AppGlobals.getPackageManager(). getPersistentApplications(STOCK_PM_FLAGS); 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); } } }
将在addAppLocked()函数中调用startProcessLocked()来启动app进程。
关于app一直存在,可实质就是拥有android:persistent=true属性的app将不能被kill或kill后会自动重启。
在AMS中搜索if (app.persistent)字段后,就可以找到问题的原因了。主要讨论下removeProcessLocked()函数:
if (app.persistent) { if (!callerWillRestart) { addAppLocked(app.info); } else { needRestart = true; } }
callerWillRestart是关键变量。
遍历所有的传入值,只有startInstrumentation()函数会将callerWillRestart设为true,此时的注释为
// Instrumentation can kill and relaunch even persistent processes。
而我们在关闭app时,例如eclipse中点击stop的时,与这个函数无关的。因此会重启android:persistent=true属性的app。
同时在killAllBackgroundProcesses()中:
// we don't kill persistent processes
而不做其他有效操作。具体细节的内容,可以log出结果。
特别注意:此处在AMS中构造的ProcessRecord对象,即上面提到的app,其成员变量persistent的初始值为false。
那仅仅在apk的AndroidManifest.xml文件中设置android:persistent=true即可?
看AMS中的实现代码:
if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) { app.persistent = true; app.maxAdj = CORE_SERVER_ADJ; }
此处获取两个关键的信息:
1.
(1).在apk的AndroidManifest.xml文件中设置android:persistent=true
(2).此apk需要放入到system/app目录下,成为一个systemapp
2.app.persistent = true不仅仅标志着此apk不能轻易的被kill掉,亦或在被kill掉后能够自动restart,并且还涉及到了进程的优先级。将被设置为CORE_SERVER_ADJ,此值为-12,而核心进程init的值为-16。当前正在前台运行的进程的值为0。
另:
在xml文件中对于Preference管理的配置,也可以使用app.persistent = true来简单的保存设置值。
- AndroidManifest中android:persistent属性研究
- android persistent属性研究
- Android persistent属性研究
- Android persistent属性研究
- android persistent属性研究
- Android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- android persistent属性研究
- Android中persistent属性用法详解
- AndroidManifest文件中android属性
- table重用机制
- Android Studio ADB not responding.//adb.exe程序启动不起来,如何处理
- 容易产生错觉的属性(文字,内容模式,内容对齐方式)
- SVN的安装使用
- 《多线程之NSOPeration/NSOperationQueue》
- AndroidManifest中android:persistent属性研究
- Photoshop 快捷键设置之进化
- Error: Failed to create feature class. Table already registered
- React Native (一) --React 入门
- PAT 1047. 编程团体赛
- Android多媒体编程
- Linux下配置Caffe及其Python接口全过程记录(Ubuntu15.10_amd64+CUDA7.5)及训练mnist数据集
- 关于statusbar
- .net中dynamic类型的使用