http://blog.csdn.net/qq_27062249/article/details/54645214
当系统重启的时候,会由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();
//省略部分代码,保留关键部分
}
现在客户有一个需求,就是在设备重启的时候不需要显示上一次保存的应用。
这个需求其实很简单哟,TaskPersister类里添加删除保存的数据的方法,在ActivityManagerService初始化的时候调用就好了,见一下代码:
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类初始化的时候调用:
[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();
//这里读取数据之前就删除
mTaskPersister.removeAllTasks();
mRecentTasks.clear();
mRecentTasks.addAll(mTaskPersister.restoreTasksLocked());
mRecentTasks.cleanupLocked(UserHandle.USER_ALL);
mTaskPersister.startPersisting();
}
//省略部分代码,保留关键部分
就这样咯,可以去看一下TaskPersister和ActivityManagerService的源码了解更深。