Android 如何避免后台音乐被low memory结束生命 M

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN
http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

 

为防止某些进程被low memory意外杀掉,可以将其加入白名单,降低误伤的概率;
一般来说,low memory killer会首先选择adj value徘徊在9~15的process去结束生命;
 
1.在ActivityManagerService.java 增加如下代码:
static final String[] mThirdPartyAppWhiteList = { "android.process.media"};
static final int [] mThirdPartyAppAdj = {7};
 
2.在ActivityManagerService.java修改如下代码:
  private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj,
            int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) {
        app.hiddenAdj = hiddenAdj;
        app.clientHiddenAdj = clientHiddenAdj;
        app.emptyAdj = emptyAdj;
 
        if (app.thread == null) {
            return false;
        }
 
        final boolean wasKeeping = app.keeping;
 
        boolean success = true;
        boolean isWLProc = false; /// M: ALPS00497111 [Volunteer free] LCA Memory Optimized
                   boolean isThirdPartyAppWhiteProcess =  false;     //add
                   int mThirdPartyAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
 
        computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll);
 
        if (app.curRawAdj != app.setRawAdj) {
            if (wasKeeping && !app.keeping) {
                // This app is no longer something we want to keep.  Note
                // its current wake lock time to later know to kill it if
                // it is not behaving well.
                BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
                synchronized (stats) {
                    app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
                            app.pid, SystemClock.elapsedRealtime());
                }
                app.lastCpuTime = app.curCpuTime;
            }
 
            app.setRawAdj = app.curRawAdj;
        }
 
        if (app.curAdj != app.setAdj) {
            /// M: LTK @{
            if (FeatureOption.MTK_MEMORY_COMPRESSION_SUPPORT) {
                if(app.curAdj > app.setAdj && (app.curAdj >= ProcessList.PREVIOUS_APP_ADJ) && app.hasShownUi) {
                    try {
                        IAmsPlus amsplus = MediatekClassFactory.createInstance(IAmsPlus.class);
                        amsplus.afterAdjAdjustment(mMainStack.convertProcessRecord(app),
                                                   app.setAdj,
                                                   app.curAdj,
                                                   mMainStack.convertLaunchRecord(mMainStack.topRunningActivityLocked(null)));
                    } catch (Exception e) {
                        Log.w(TAG, "[LTK] Exception thrown during afterAdjAdjustment failed:", e);
                    }
                }
            }
            /// @}
 
            /// M: ALPS00497111 [Volunteer free] LCA Memory Optimized @{
            if(FeatureOption.MTK_LCA_RAM_OPTIMIZE)
            {
                for(int num = 0; num <= WL_PROCNAME.length -1 ;num++)
                {
                    if(WL_PROCNAME[num].equals(app.processName) &&
                        app.curAdj > ProcessList.HIDDEN_APP_MIN_ADJ)
                    {
                        isWLProc = true;
                        break;
                    }
                }
            }
 
//add 如下的code           
 
                            if(FeatureOption.MTK_LCA_RAM_OPTIMIZE)
            { 
                                if (mThirdPartyAppWhiteList.length != mThirdPartyAppAdj.length)
                                     {
                                               throw new Exception("mThirdPartyAppWhiteList is not match mThirdPartyAppAdj");
                                     }
                                for(int num = 0; num <= mThirdPartyAppWhiteList.length -1 ;num++)
                {
                    if(mThirdPartyAppWhiteList[num].equals(app.processName) &&
                        app.curAdj > mThirdPartyAppAdj[num])
                    {
                        isThirdPartyAppWhiteProcess = true;
                                                        mThirdPartyAdj = mThirdPartyAppAdj[num];                      
                    }
                }
            }
//add code  end
                           
            if(isWLProc)
            {
                if (Process.setOomAdj(app.pid, ProcessList.HIDDEN_APP_MIN_ADJ)) {
                    if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                        TAG, "Set " + app.pid + " " + app.processName +
                        " adj " + ProcessList.HIDDEN_APP_MIN_ADJ + ": " + app.adjType);
                    app.setAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
                } else {
                    success = false;
                    Slog.w(TAG, "Failed setting oom adj of " + app + " to " + ProcessList.HIDDEN_APP_MIN_ADJ);
                }
            }
//添加如下的判断:
                            else if(isThirdPartyAppWhiteProcess)
                            {
                           
                             if (Process.setOomAdj(app.pid, mThirdPartyAdj)) {
                    if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                        TAG, "Set " + app.pid + " " + app.processName +
                        " adj " + mThirdPartyAdj + ": " + app.adjType);
                    app.setAdj = mThirdPartyAdj;
                } else {
                    success = false;
                    Slog.w(TAG, "Failed setting oom adj of " + app + " to " + mThirdPartyAdj);
                }                          
                            }                          
            else
            {
                if (Process.setOomAdj(app.pid, app.curAdj)) {
                    if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                        TAG, "Set " + app.pid + " " + app.processName +
                        " adj " + app.curAdj + ": " + app.adjType);
                    app.setAdj = app.curAdj;
                } else {
                    success = false;
                    Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj);
                }
            }
            /// @}
        }
        if (app.setSchedGroup != app.curSchedGroup) {
            app.setSchedGroup = app.curSchedGroup;
            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
                    "Setting process group of " + app.processName
                    + " to " + app.curSchedGroup);
            if (app.waitingToKill != null &&
                    app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
                Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill);
                EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid,
                        app.processName, app.setAdj, app.waitingToKill);
                app.killedBackground = true;
                Process.killProcessQuiet(app.pid);
                success = false;
            } else {
                if (true) {
                    long oldId = Binder.clearCallingIdentity();
                    try {
                        Process.setProcessGroup(app.pid, app.curSchedGroup);
                    } catch (Exception e) {
                        Slog.w(TAG, "Failed setting process group of " + app.pid
                                + " to " + app.curSchedGroup);
                        e.printStackTrace();
                    } finally {
                        Binder.restoreCallingIdentity(oldId);
                    }
                } else {
                    if (app.thread != null) {
                        try {
                            app.thread.setSchedulingGroup(app.curSchedGroup);
                        } catch (RemoteException e) {
                        }
                    }
                }
            }
        }
        return success;
    }


你可能感兴趣的:(Android,MTK)