Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程

Posted 阿迷创客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程相关的知识,希望对你有一定的参考价值。

一、前言

最近开发过程中,因系统内存相对比较紧张,在启动某些CPU、内存占用比较大的APP的时候,就需要清理一下,把能杀的都杀掉,给前台进程腾出系统资源。

  • 缺陷:对于persist进程,以及一些流氓APP(杀死后还自己起来的),还得想别的办法处理

二、代码实现

2.1 权限部分

  • 要杀死其他APP进程,需要拥有下面权限,请添加到 androidManifest.xml 的权限声明列表中
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

2.2 获取正在运行的APP列表

  • 在Andorid P的版本里,下面的API接口,只有拥有"android.uid.system"、系统签名的APP上才能正常使用
  • 如果不具备"android.uid.system",则只能获取到自己当前进程的名称字符串

2.2.1 设置 android:sharedUserId

  • 修改AndroidManifest.xml的开头部分,添加 android:sharedUserId=“android.uid.system”
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:sharedUserId="android.uid.system"
    package="com.future.xyz.multimediacenter">

2.2.2 获取进程列表

  • 如下,使用activityManager .getRunningAppProcesses()接口获取
 ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        List<ActivityManager.RunningAppProcessInfo> list = activityManager .getRunningAppProcesses();
        int i = list.size();
        Log.i(TAG, String.valueOf(i));
        for (int j = 0; j < list.size(); j++) 
            Log.i(TAG,"zs, processName: "+ list.get(j).processName);
        

2.3 设置白名单

  • 将不需要杀死的APP的包名添加进去
  private String mProcWhiteList[] =          
            ,"com.android.providers.tv"
            ,"com.mediatek.tvinput"
            ,"com.dangbei.lerad.vesuvius.sample"
//            ,"com.ktcp.tvvideo"
            ,"com.android.browser"
            ,"com.hisense.client"
            ,"com.android.soundrecorder"
//            ,"com.zk.tvwizard"
            ,"com.android.inputmethod.pinyin"
            ,"com.android.defcontainer"
            ,"system"
            ,"com.ms.weshare"
            // …… 省略……
    ;

2.4 杀死白名单外的进程

  • 如下在当前APP启动的onCreate阶段,先杀一遍
 Log.d(TAG, "zs,  onCreate ");
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        List<ActivityManager.RunningAppProcessInfo> list = activityManager .getRunningAppProcesses();
        int i = list.size();
        Log.i(TAG, String.valueOf(i));
        for (int j = 0; j < list.size(); j++) 
            Log.i(TAG,"zs, processName: "+ list.get(j).processName);
            Boolean killProc = true;
            for(int k=0; k<mProcWhiteList.length; k++)
                if(mProcWhiteList[k].equals(list.get(j).processName))
                    killProc = false;
                
            
            if(killProc)
                activityManager.killBackgroundProcesses(list.get(j).processName);
                Log.d(TAG, "zs,  killed  "+list.get(j).processName);
            
        

2.5 打印

2022-10-19 16:46:38.859 6390-6390/com.future.xyz.multimediacenter D/DeviceListActivity: zs,  onCreate 
2022-10-19 16:46:38.860 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.dangbei.mimir.lightos.home
2022-10-19 16:46:38.860 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.future.xyz.multimediacenter
2022-10-19 16:46:38.861 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.mediatek.wwtv.tvcenter
2022-10-19 16:46:38.861 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.keychain
2022-10-19 16:46:38.861 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.defcontainer
2022-10-19 16:46:38.861 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.utv.android
2022-10-19 16:46:38.872 6390-6390/com.future.xyz.multimediacenter D/DeviceListActivity: zs,  killed  com.utv.android
2022-10-19 16:46:38.872 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.mediatek.network
2022-10-19 16:46:38.872 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.kingboard.settings
2022-10-19 16:46:38.877 6390-6390/com.future.xyz.multimediacenter D/DeviceListActivity: zs,  killed  com.zk.settings
2022-10-19 16:46:38.877 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.inputmethod.latin
2022-10-19 16:46:38.877 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.fanzc
2022-10-19 16:46:38.877 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.zk.tvass
2022-10-19 16:46:38.877 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.tv.settings
2022-10-19 16:46:38.878 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.dangbei.lerad.vesuvius.sample
2022-10-19 16:46:38.878 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.talkingdata.sdk.TDAntiCheatingService
2022-10-19 16:46:38.878 6390-6390/com.future.xyz.multimediacenter D/DeviceListActivity: zs,  killed  com.talkingdata.sdk.TDAntiCheatingService
2022-10-19 16:46:38.878 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.bluetooth
2022-10-19 16:46:38.878 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.mediatek.tvinput
2022-10-19 16:46:38.878 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.ms.weshare
2022-10-19 16:46:38.879 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.mediatek.tvinputservice.arbitratorservice
2022-10-19 16:46:38.881 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.mediatek.TimeMeasurementAgent
2022-10-19 16:46:38.881 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.mediatek.hotkey.dispatcher
2022-10-19 16:46:38.881 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.se
2022-10-19 16:46:38.881 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.zk.extracontrol
2022-10-19 16:46:38.882 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.dangbei.speech
2022-10-19 16:46:38.882 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.aispeech.tvui
2022-10-19 16:46:38.882 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: com.android.systemui
2022-10-19 16:46:38.883 6390-6390/com.future.xyz.multimediacenter I/DeviceListActivity: zs, processName: system

三、结尾

对于persist进程(自启动),以及一些流氓APP(杀死后还自己起来的),还得想别的办法处理。

以上是关于Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程的主要内容,如果未能解决你的问题,请参考以下文章

Android中的Doze模式

Android VoIP 应用程序:PARTIAL_WAKE_LOCK 与忽略电池优化(打瞌睡白名单)

2021Android App开发工作必知必会之性能优化

2021Android App开发工作必知必会之性能优化

2021Android App开发工作必知必会之性能优化

Android性能优化第(八)篇---App启动速度优化之耗时检测处理