Android9.0 添加APP后台防杀机制

软件平台:Android9.0

硬件平台:MTK6771

开发过程中遇到在系统资源紧张的时候,后台进程被LMK杀死的情况,下面提供一个防杀策略,供参考:

diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 65d75784..bf3bd533 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -24675,6 +24675,16 @@ public class ActivityManagerService extends IActivityManager.Stub
         int changes = 0;
 
         if (app.curAdj != app.setAdj) {
+            // kbding add this for preventing ocr killing by lmk.
+            String[] packages = app.getPackageList();
+            if (packages != null) {
+                for (String p : packages){
+                    if(p.equals("com.iflytek.ocr")){
+                        app.curAdj = -1;
+                        break;
+                    }
+                }
+            }

             ProcessList.setOomAdj(app.pid, app.uid, app.curAdj);
             if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mCurOomAdjUid == app.info.uid) {
                 String msg = "Set " + app.pid + " " + app.processName + " adj "

过滤需要防杀的包名,将其curAdj赋值,这个值越大,LMK在清理内存的时候就会优先清理,因此赋值-1防杀。

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(android)