Android应用具有persist属性时如何自升级

                  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对各种组件的状态记忆并重新启动该应用。

               





       

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值