高通Android 12应用保活时序问题踩坑

java.lang.RuntimeException: No data directory found for package android
                                                                                                    	at android.app.ContextImpl.getDataDir(ContextImpl.java:2916)
                                                                                                    	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:696)
                                                                                                    	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:923)
                                                                                                    	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:545)
                                                                                                    	at com.android.server.am.ActivityManagerService.getWhiteAppProcessList(ActivityManagerService.java:1903)
                                                                                                    	at com.android.server.am.OomAdjuster.isInWhitelist(OomAdjuster.java:2696)
                                                                                                    	at com.android.server.am.OomAdjuster.applyOomAdjLSP(OomAdjuster.java:2782)
                                                                                                    	at com.android.server.am.OomAdjuster.updateAndTrimProcessLSP(OomAdjuster.java:1264)
                                                                                                    	at com.android.server.am.OomAdjuster.updateOomAdjInnerLSP(OomAdjuster.java:1038)
                                                                                                    	at com.android.server.am.OomAdjuster.performUpdateOomAdjLSP(OomAdjuster.java:629)
                                                                                                    	at com.android.server.am.OomAdjuster.updateOomAdjLSP(OomAdjuster.java:615)
                                                                                                    	at com.android.server.am.OomAdjuster.updateOomAdjLocked(OomAdjuster.java:603)
                                                                                                    	at com.android.server.am.ActivityManagerService.updateOomAdjLocked(ActivityManagerService.java:14869)
                                                                                                    	at com.android.server.am.ActivityManagerService.setSystemProcess(ActivityManagerService.java:1851)
                                                                                                    	at com.android.server.SystemServer.startBootstrapServices(SystemServer.java:1225)
                                                                                                    	at com.android.server.SystemServer.run(SystemServer.java:887)
                                                                                                    	at com.android.server.SystemServer.main(SystemServer.java:620)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

2、一开始我们是封装在自定义系统服务里面,后面更换时序位置,通过一个工具类去调用。解决这个问题。所以简单记录下。代码如下图所示

 public static List<String> getWhiteAppProcessList() {
        List<String> list = new ArrayList();

        String sizeKey = "persist.xxx.aliveapps_s";
        String valKey = "persist.xxx.aliveapps_";
        int size = 0;
        try
        {
            size = Integer.parseInt(SystemProperties.get(sizeKey));
        }
        catch(Exception e)
        {
        }
        if(size>0)
        {
            for(int i=0;i<size;i++)
            {
                list.add(SystemProperties.get(valKey+i));
            }
        }
        return list;
    }

    public static void setWhiteAppProcessList(List<String> whiteAppProcessList) {
        if(whiteAppProcessList==null || whiteAppProcessList.size()==0)
        {
            whiteAppProcessList = new ArrayList();
        }


        String sizeKey = "persist.xxx.aliveapps_s";
        String valKey = "persist.xxx.aliveapps_";
        int orgSize = 0;
        try
        {
            orgSize = Integer.parseInt(SystemProperties.get(sizeKey));
        }
        catch(Exception e)
        {

        }
        if (orgSize > 0) {
            for(int i=0;i<orgSize;i++)
            {
                SystemProperties.set(valKey+i,"");
            }
        }

        int size = whiteAppProcessList.size();
        SystemProperties.set(sizeKey,String.valueOf(size));
        for(int i=0;i<size;i++)
        {
            SystemProperties.set(valKey+i,whiteAppProcessList.get(i));
        }

    }


    public static List<String> getBackgroundAliveAppList() {

        String sizeKey = "persist.xxx.backgroundapps_s";
        String valKey = "persist.xxx.backgroundapps_";
        List<String> list = new ArrayList();
        int size = 0;
        try
        {
            size = Integer.parseInt(SystemProperties.get(sizeKey));
        }
        catch(Exception e)
        {
        }
        if(size>0)
        {
            for(int i=0;i<size;i++)
            {
                list.add(SystemProperties.get(valKey+i));
            }
        }
        return list;
    }

    public static void setBackgroundAliveAppList(List<String> backgroundAliveAppList) {
        if(backgroundAliveAppList==null || backgroundAliveAppList.size()==0)
        {
            backgroundAliveAppList = new ArrayList();
        }

        String sizeKey = "persist.xxx.backgroundapps_s";
        String valKey = "persist.xxx.backgroundapps_";
        int orgSize = 0;
        try
        {
            orgSize = Integer.parseInt(SystemProperties.get(sizeKey));
        }
        catch(Exception e)
        {

        }
        if (orgSize > 0) {
            for(int i=0;i<orgSize;i++)
            {
                SystemProperties.set(valKey+i,"");
            }
        }

        int size = backgroundAliveAppList.size();
        SystemProperties.set(sizeKey,String.valueOf(size));
        for(int i=0;i<size;i++)
        {
            SystemProperties.set(valKey+i,backgroundAliveAppList.get(i));
        }

    }

转载请注明出处高通Android 12应用保活时序问题踩坑-CSDN博客,谢谢!

相关推荐
豆 腐2 小时前
MySQL【四】
android·数据库·笔记·mysql
想取一个与众不同的名字好难4 小时前
android studio导入OpenCv并改造成.kts版本
android·ide·android studio
Jewel1054 小时前
Flutter代码混淆
android·flutter·ios
Yawesh_best6 小时前
MySQL(5)【数据类型 —— 字符串类型】
android·mysql·adb
曾经的三心草8 小时前
Mysql之约束与事件
android·数据库·mysql·事件·约束
guoruijun_2012_412 小时前
fastadmin多个表crud连表操作步骤
android·java·开发语言
Winston Wood12 小时前
一文了解Android中的AudioFlinger
android·音频
B.-14 小时前
Flutter 应用在真机上调试的流程
android·flutter·ios·xcode·android-studio
有趣的杰克14 小时前
Flutter【04】高性能表单架构设计
android·flutter·dart
大耳猫19 小时前
主动测量View的宽高
android·ui