Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 如何避免後台音樂被low memory結束生命 M

Android 如何避免後台音樂被low memory結束生命 M

編輯:關於Android編程

 

正文

 

 

為防止某些進程被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;
    }


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved