应用保活

概述

在某些场景下,我们想让某些app能一直在前台或者后台运行,而不被Android系统杀死.这时就涉及到了应用保活.应用保活可以从两个方向来实现,一个是应用层,一个是系统层.

Android系统进程优先级

应用保活从本质上来讲就是提高进程的优先级.详细可以参考以下博客:

https://www.jianshu.com/p/4ee14aa23f07

下面是Android定义的ADJ值.

在这里插入图片描述

应用保活

对用开发app的公司来说,由于没有系统源码,所以只能从app层面来考虑保活;但是对于rom定制厂商来说就可以从系统层实现app保活.

应用层方式

应用层保活可以参考下面的博客:

https://blog.csdn.net/qq_30993595/article/details/90678273

该博客详细介绍了几种主流的应用保活方式.

系统层方式

对于没有白名单机制的Android版本

--- a/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -518,6 +518,9 @@ public class ActivityManagerService extends IActivityManager.Stub
 
     static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
 
+    // hefang add for disable kill service
+    public static final boolean EMDOOR_IMORAN_APPS_SUPPORT = (SystemProperties.get("ro.emdoor_imoran_apps_support", "0").equals("1"));
+
     /// M: log enhancement @{
     static final boolean IS_USER_DEBUG_BUILD = "userdebug".equals(Build.TYPE);
     static final boolean IS_USER_BUILD = "user".equals(Build.TYPE) || IS_USER_DEBUG_BUILD;
@@ -22211,10 +22214,29 @@ public class ActivityManagerService extends IActivityManager.Stub
         int changes = 0;
 
         if (app.curAdj != app.setAdj) {
+            // hefang add for disable kill service
+            if(EMDOOR_IMORAN_APPS_SUPPORT) {
+                String PKG_MORAPP = "net.imoran.main.launcher";
+                String PKG_MORSPEAKER = "net.imoran.voice";
+                String PKG_DOCTOR= "com.joyhealth.intelligencefamilydoctor";
+                String[] packages = app.getPackageList();
+                if(packages != null) {
+                    for(String p : packages) {
+                        if(p.equals(PKG_MORAPP)
+                                ||p.equals(PKG_MORSPEAKER)
+                                ||p.equals(PKG_DOCTOR)) {
+                            android.util.Log.d("hefang_lowKill", "set adj -1");
+                            app.curAdj = -1;
+                            break;
+                        }
+                    }
+                }
+            }
+            //add end
             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 "
-                        + app.curAdj + ": " + app.adjType;
+                    + app.curAdj + ": " + app.adjType;
                 reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
             }
             app.setAdj = app.curAdj;

对于有白名单机制的Android版本:

--- /dev/null
+++ b/vendor/emdoor/imoran/configuration.xml
@@ -0,0 +1,8 @@
+
+
+    
+        net.imoran.voice
+       net.imoran.main.launcher
+       com.joyhealth.intelligencefamilydoctor
+    
+
diff --git a/vendor/emdoor/imoran/imoran.mk b/vendor/emdoor/imoran/imoran.mk
index 64291b9..ccbb349 100755
--- a/vendor/emdoor/imoran/imoran.mk
+++ b/vendor/emdoor/imoran/imoran.mk
@@ -18,3 +18,4 @@ PRODUCT_PACKAGES += $(MODULE_MORAPP)
 #$(shell cp vendor/emdoor/imoran/$(MODULE_MORAPP)/lib out/target/product/$(MTK_TARGET_PROJECT)/obj/APPS/$(MODULE_MORAPP)/lib/arm)
 PRODUCT_COPY_FILES += $(call find-copy-subdir-files,*,vendor/emdoor/imoran/$(MODULE_MORAPP)/lib,system/app/$(MODULE_MORAPP)/lib/arm)
 
+PRODUCT_COPY_FILES += vendor/emdoor/imoran/configuration.xml:system/etc/imoran/configuration.xml

查看进程的adj值

adb shell

ps|grep "进程名"

cat /proc/进程名对应PID/oom_adj

你可能感兴趣的:(应用保活)