關(guān)于Android重啟機器,之前的任務(wù)還是會顯示出來的修改

http://blog.csdn.net/qq_27062249/article/details/54645214

當系統(tǒng)重啟的時候,會由ActivityManagerService操作TaskPersister類的restoreTasksLocked方法去讀取,見下面代碼:
ActivityManagerService在源碼中的位置:./frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java:

[java] view plain copy

public void systemReady(final Runnable goingCallback) {

   synchronized(this) {  
       if (mSystemReady) {  
           // If we're done calling all the receivers, run the next "boot phase" passed in  
           // by the SystemServer  
           if (goingCallback != null) {  
               goingCallback.run();  
           }  
           return;  
       }  

       mLocalDeviceIdleController  
               = LocalServices.getService(DeviceIdleController.LocalService.class);  

       // Make sure we have the current profile info, since it is needed for  
       // security checks.  
       updateCurrentProfileIdsLocked();  
       mRecentTasks.clear();  
       mRecentTasks.addAll(mTaskPersister.restoreTasksLocked());  
       mRecentTasks.cleanupLocked(UserHandle.USER_ALL);  
       mTaskPersister.startPersisting();  
//省略部分代碼,保留關(guān)鍵部分  

}

現(xiàn)在客戶有一個需求,就是在設(shè)備重啟的時候不需要顯示上一次保存的應(yīng)用。
這個需求其實很簡單喲,TaskPersister類里添加刪除保存的數(shù)據(jù)的方法,在ActivityManagerService初始化的時候調(diào)用就好了,見一下代碼:
TaskPersister里添加刪除的方法:

[java] view plain copy

private void removeAllTasks(File[] files) {
if (files == null) {
Slog.e(TAG, "File error accessing recents directory (too many files open?).");
return;
}

 for (int fileNdx = 0; fileNdx < files.length; ++fileNdx) {  
     File file = files[fileNdx];  
     file.delete();  
 }  

}

public void removeAllTasks() {

 removeAllTasks(sTasksDir.listFiles());  
 removeAllTasks(sImagesDir.listFiles());  

}

ActivityManagerService類初始化的時候調(diào)用:

[java] view plain copy

public void systemReady(final Runnable goingCallback) {
synchronized(this) {
if (mSystemReady) {
// If we're done calling all the receivers, run the next "boot phase" passed in
// by the SystemServer
if (goingCallback != null) {
goingCallback.run();
}
return;
}

       mLocalDeviceIdleController  
               = LocalServices.getService(DeviceIdleController.LocalService.class);  

       // Make sure we have the current profile info, since it is needed for  
       // security checks.  
       updateCurrentProfileIdsLocked();  

//這里讀取數(shù)據(jù)之前就刪除
mTaskPersister.removeAllTasks();

       mRecentTasks.clear();  
       mRecentTasks.addAll(mTaskPersister.restoreTasksLocked());  
       mRecentTasks.cleanupLocked(UserHandle.USER_ALL);  
       mTaskPersister.startPersisting();  

}
//省略部分代碼,保留關(guān)鍵部分

就這樣咯,可以去看一下TaskPersister和ActivityManagerService的源碼了解更深。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容