• 通过修改源码解决低内存杀死自己app的解决方案


    通过修改源码解决低内存杀死自己app的解决方案:
    修改源码路径:frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java

    private final int computeOomAdjLocked(ProcessRecord app, int cachedAdj, ProcessRecord TOP_APP, boolean doingAll, long now)

    在 computeOomAdjLocked 添加下面代码。红色代码为添加代码

    // Examine all activities if not already foreground.
            if (!foregroundActivities && activitiesSize > 0) {
                int minLayer = ProcessList.VISIBLE_APP_LAYER_MAX;
                for (int j = 0; j < activitiesSize; j++) {
                    final ActivityRecord r = app.activities.get(j);
                    if (r.app != app) {
                        Log.e(TAG, "Found activity " + r + " in proc activity list using " + r.app
                                + " instead of expected " + app);
                        if (r.app == null || (r.app.uid == app.uid)) {
                            // Only fix things up when they look sane
                            r.app = app;
                        } else {
                            continue;
                        }
                    }
                    if (r.visible) {
                        // App has a visible activity; only upgrade adjustment.
                        if (adj > ProcessList.VISIBLE_APP_ADJ) {
                            adj = ProcessList.VISIBLE_APP_ADJ;
                            app.adjType = "visible";
                        }
                        if (procState > PROCESS_STATE_CUR_TOP) {
                            procState = PROCESS_STATE_CUR_TOP;
                        }
                        schedGroup = ProcessList.SCHED_GROUP_DEFAULT;
                        app.cached = false;
                        app.empty = false;
                        foregroundActivities = true;
                        if (r.task != null && minLayer > 0) {
                            final int layer = r.task.mLayerRank;
                            if (layer >= 0 && minLayer > layer) {
                                minLayer = layer;
                            }
                        }
                        break;
                    } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) {
                        if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
                            adj = ProcessList.PERCEPTIBLE_APP_ADJ;
                            app.adjType = "pausing";
                        }
                        if (procState > PROCESS_STATE_CUR_TOP) {
                            procState = PROCESS_STATE_CUR_TOP;
                        }
                        schedGroup = ProcessList.SCHED_GROUP_DEFAULT;
                        app.cached = false;
                        app.empty = false;
                        foregroundActivities = true;
                    } else if (r.state == ActivityState.STOPPING) {
                        if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
                            adj = ProcessList.PERCEPTIBLE_APP_ADJ;
                            app.adjType = "stopping";
                        }
                        // For the process state, we will at this point consider the
                        // process to be cached.  It will be cached either as an activity
                        // or empty depending on whether the activity is finishing.  We do
                        // this so that we can treat the process as cached for purposes of
                        // memory trimming (determing current memory level, trim command to
                        // send to process) since there can be an arbitrary number of stopping
                        // processes and they should soon all go into the cached state.
                        if (!r.finishing) {
                            if (procState > ActivityManager.PROCESS_STATE_LAST_ACTIVITY) {
                                procState = ActivityManager.PROCESS_STATE_LAST_ACTIVITY;
                            }
                        }
                        app.cached = false;
                        app.empty = false;
                        foregroundActivities = true;
                    } else {
                        if (procState > ActivityManager.PROCESS_STATE_CACHED_ACTIVITY) {
                            procState = ActivityManager.PROCESS_STATE_CACHED_ACTIVITY;
                            app.adjType = "cch-act";
                        }
                    }
                }
                if (adj == ProcessList.VISIBLE_APP_ADJ) {
                    adj += minLayer;
                }
            }


     if(app.processName.equals("不想被杀死的app的包名")){
           adj = ProcessList.VISIBLE_APP_ADJ;
     }

    以上为添加的源码 

    if (adj > ProcessList.PERCEPTIBLE_APP_ADJ
                    || procState > ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
                if (app.foregroundServices) {
                    // The user is aware of this app, so make it visible.
                    adj = ProcessList.PERCEPTIBLE_APP_ADJ;
                    procState = ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
                    app.cached = false;
                    app.adjType = "fg-service";
                    schedGroup = ProcessList.SCHED_GROUP_DEFAULT;
                } else if (app.forcingToForeground != null) {
                    // The user is aware of this app, so make it visible.
                    adj = ProcessList.PERCEPTIBLE_APP_ADJ;
                    procState = ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
                    app.cached = false;
                    app.adjType = "force-fg";
                    app.adjSource = app.forcingToForeground;
                    schedGroup = ProcessList.SCHED_GROUP_DEFAULT;
                }
            }

  • 相关阅读:
    Revit中墙体的连接方式创建,快速改变墙连接状态
    C++数据结构X篇_01_数据结构的基本概念
    微服务架构|go-zero 的自适应熔断器
    nginx常见报错及解决acme.sh给Nginx配置SSL证书
    【CSDN 每日一练 ★★☆】【字符串】外观数列
    python打印字符串中的大写字符
    电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
    聊聊室内导航在应用方面
    (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
    闵帆老师《论文写作》课程之心得体会
  • 原文地址:https://blog.csdn.net/m0_37039192/article/details/110371573