Android应用具有persist属性时如何自升级
2017-01-07 21:37
1346 查看
Android系统中,为了某些目的需要保证应用运行时尽量不被系统kill(特别是处于后台时),所以都会给应用增加persist标签,以避免
在系统低内存时被系统kill。但是添加了该属性的应用如果需要进行应用自升级(无论是别的应用进行安装升级还是应用自己安装自己进行应用升级),有一些需要特别注意的地方,否则就会导致程序的运行状态错乱。
一般没有persist属性的应用在进行应用自升级的时候,系统会保证程序运行的状态是OK的,因为在安装应用(应用自升级)的过程中,系统会KILL此应用的进程,同时清理之前在AMS(ActivityManagerService)中记录的此应用进程中的各种组件(Activity,Service,Receiver,ContentProvider),在下次该新版本的应用重新启动时,系统会重新new一个进程并重新加载各种组件运行,所以不会有问题;当应用有persist属性并且生效时(不但要添加persist标签,还要是系统签名或者有系统权限),系统安装新应用的过程与安装不带此属性的过程不同,主要在是因为:
系统不会KILL此应用的进程,代码如下:
private final boolean killPackageProcessesLocked(String packageName, int appId,
int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
boolean doit, boolean evenPersistent, String reason) {
ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
// Remove all processes this package may have touched: all with the
// same UID (except for the system or root user), and all whose name
// matches the package name.
final int NP = mProcessNames.getMap().size();
for (int ip=0; ip<NP; ip++) {
SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
final int NA = apps.size();
for (int ia=0; ia<NA; ia++) {
ProcessRecord app = apps.valueAt(ia);
if (app.persistent && !evenPersistent) {
// we don't kill persistent processes
continue;(如果有persist属性而且没有指定要KILL带由此属性的标记的,则跳过)
}
if (app.removed) {
if (doit) {
procs.add(app);
}
continue;
}
// Skip process if it doesn't meet our oom adj requirement.
if (app.setAdj < minOomAdj) {
continue;
}
// If no package is specified, we call all processes under the
// give user id.
if (packageName == null) {
if (app.userId != userId) {
continue;
}
if (appId >= 0 && UserHandle.getAppId(app.uid) != appId) {
continue;
}
// Package has been specified, we want to hit all processes
// that match it. We need to qualify this by the processes
// that are running under the specified app and user ID.
} else {
final boolean isDep = app.pkgDeps != null
&& app.pkgDeps.contains(packageName);
if (!isDep && UserHandle.getAppId(app.uid) != appId) {
continue;
}
if (userId != UserHandle.USER_ALL && app.userId != userId) {
continue;
}
if (!app.pkgList.containsKey(packageName) && !isDep) {
continue;
}
}
// Process has passed all conditions, kill it!
if (!doit) {
return true;
}
app.removed = true;
procs.add(app);
}
}
int N = procs.size();
for (int i=0; i<N; i++) {
removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
}
updateOomAdjLocked();
return N > 0;
}
系统不会清理之前在AMS(ActivityManagerService)中记录的各种组件的信息,只会把新版应用中的各种组件记录到AMS中。
这样的话如果前后2个版本的应用的某个组件(如Actvivity)名称没有变化,但是内部增加了很多新的类和新的逻辑实现,就会导致加载的组件有的是新版本的有的是老版本,导致程序功能错乱。一种解决的方法如下:
接收新版应用安装完成的广播,调用 context的startInstrumentation 方法,该方法会强制系统KILL应用进程并清理AMS对各种组件的状态记忆并重新启动该应用。
在系统低内存时被系统kill。但是添加了该属性的应用如果需要进行应用自升级(无论是别的应用进行安装升级还是应用自己安装自己进行应用升级),有一些需要特别注意的地方,否则就会导致程序的运行状态错乱。
一般没有persist属性的应用在进行应用自升级的时候,系统会保证程序运行的状态是OK的,因为在安装应用(应用自升级)的过程中,系统会KILL此应用的进程,同时清理之前在AMS(ActivityManagerService)中记录的此应用进程中的各种组件(Activity,Service,Receiver,ContentProvider),在下次该新版本的应用重新启动时,系统会重新new一个进程并重新加载各种组件运行,所以不会有问题;当应用有persist属性并且生效时(不但要添加persist标签,还要是系统签名或者有系统权限),系统安装新应用的过程与安装不带此属性的过程不同,主要在是因为:
系统不会KILL此应用的进程,代码如下:
private final boolean killPackageProcessesLocked(String packageName, int appId,
int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
boolean doit, boolean evenPersistent, String reason) {
ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
// Remove all processes this package may have touched: all with the
// same UID (except for the system or root user), and all whose name
// matches the package name.
final int NP = mProcessNames.getMap().size();
for (int ip=0; ip<NP; ip++) {
SparseArray<ProcessRecord> apps = mProcessNames.getMap().valueAt(ip);
final int NA = apps.size();
for (int ia=0; ia<NA; ia++) {
ProcessRecord app = apps.valueAt(ia);
if (app.persistent && !evenPersistent) {
// we don't kill persistent processes
continue;(如果有persist属性而且没有指定要KILL带由此属性的标记的,则跳过)
}
if (app.removed) {
if (doit) {
procs.add(app);
}
continue;
}
// Skip process if it doesn't meet our oom adj requirement.
if (app.setAdj < minOomAdj) {
continue;
}
// If no package is specified, we call all processes under the
// give user id.
if (packageName == null) {
if (app.userId != userId) {
continue;
}
if (appId >= 0 && UserHandle.getAppId(app.uid) != appId) {
continue;
}
// Package has been specified, we want to hit all processes
// that match it. We need to qualify this by the processes
// that are running under the specified app and user ID.
} else {
final boolean isDep = app.pkgDeps != null
&& app.pkgDeps.contains(packageName);
if (!isDep && UserHandle.getAppId(app.uid) != appId) {
continue;
}
if (userId != UserHandle.USER_ALL && app.userId != userId) {
continue;
}
if (!app.pkgList.containsKey(packageName) && !isDep) {
continue;
}
}
// Process has passed all conditions, kill it!
if (!doit) {
return true;
}
app.removed = true;
procs.add(app);
}
}
int N = procs.size();
for (int i=0; i<N; i++) {
removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
}
updateOomAdjLocked();
return N > 0;
}
系统不会清理之前在AMS(ActivityManagerService)中记录的各种组件的信息,只会把新版应用中的各种组件记录到AMS中。
这样的话如果前后2个版本的应用的某个组件(如Actvivity)名称没有变化,但是内部增加了很多新的类和新的逻辑实现,就会导致加载的组件有的是新版本的有的是老版本,导致程序功能错乱。一种解决的方法如下:
接收新版应用安装完成的广播,调用 context的startInstrumentation 方法,该方法会强制系统KILL应用进程并清理AMS对各种组件的状态记忆并重新启动该应用。
相关文章推荐
- android如何调用其他应用的方法或属性
- Android应用如何判断系统升级了?
- Android-->如何让普通View具有checked属性(背景xml支持checked状态)
- android应用客户端自动升级代码
- Android高手进阶教程(十二)之----Android 在一个应用中如何启动另外一个已安装的应用!!!
- 如何升级基于STL的应用来支持Unicode
- 如何使用Android SDK开发Android应用?
- 在GridView中LinkButton的属性的应用(如何不用选中就删除这一行)
- android应用客户端自动升级
- 如何使用Android SDK开发Android应用
- 如何使用Android SDK开发Android应用
- 如何设置隐藏Android应用的标题栏和状态栏
- 【翻译文章】如何升级基于STL的应用来支持Unicode
- Android高手进阶教程(十二)之----Android 在一个应用中如何启动另外一个已安装的应用!
- 如何在DataGrid中添加一列CheckedListBox用作选取该列和复选多列---WinForm DataGrid的TableStyles(表样式)属性应用
- 如何正常关闭android应用
- 在GridView中LinkButton的属性的应用(如何不用选中就删除这一行)
- 前面在《Asp.Net Forums中对.Net中序列化和反序列化的应用》一文中讲了,对于一些扩展属性,可以将字符串集合序列化为二进制,也可以从二进制反序列化为字符串集合。其实我一直有个疑问,对于asp.net中可以很容易实现,但是在asp中该如何?
- 如何使用Android SDK开发Android应用?
- android应用客户端自动升级