我正在写一个启动器,它需要从系统中清除最近的应用程序/任务列表,而不是“没有在最近的任务列表中显示我的应用程序”,但我现在不知道。我在stackoverflow中搜索过,只有这个问题匹配,但答案没有任何帮助。其他人也问过同样的问题,他提到了来自 Android 4.0 的 RemoveTask。是的,我已经检查了 Android 2.3.7 和 Android 4.0 的源代码,大致估计,如果我可以删除 ActivityMangerService.Java 中定义的 mRecentTasks 列表,我想我几乎可以到达终点:
final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();
另一个可能有用的定义:
static ActivityManagerService mSelf;
public static ActivityManagerService self() {
return mSelf;
}
因为我不熟悉 Java&refelction,所以我需要有关清除此列表的方式的帮助,以下是我的代码:
public static <T> void clearRecentTaskList(Launcher launcher){
ActivityManager am = (ActivityManager) launcher.getSystemService(Context.ACTIVITY_SERVICE);
Object systemRecentTask = new ArrayList<T>();
Object receiver = null;
Field recentTaskList = null;
Class<?> service = null;
Field self = null;
try {
service = Class.forName("com.android.server.am.ActivityManagerService");
Log.d(LOG_TAG, "clearRecentTaskList, service gotton"+service.getName());
} catch (ClassNotFoundException e2) {
Log.d(LOG_TAG, "clearRecentTaskList, class service not found");
}
try {
self = service.getDeclaredField("mSelf");
} catch (SecurityException e2) {
Log.d(LOG_TAG, "clearRecentTaskList, SecurityException during get mSelf");
} catch (NoSuchFieldException e2) {
Log.d(LOG_TAG, "clearRecentTaskList, NoSuchFieldException during get mSelf");
}
Log.d(LOG_TAG, "clearRecentTaskList, self gotton " + self.toGenericString());
try {
self.setAccessible(true);
receiver = self.get(null);
} catch (IllegalArgumentException e2) {
Log.d(LOG_TAG, "clearRecentTaskList, IllegalArgumentException during use self to get the receiver");
} catch (IllegalAccessException e2) {
Log.d(LOG_TAG, "clearRecentTaskList, IllegalAccessException during use self to get the receiver");
}
if ( receiver != null){
Log.d(LOG_TAG, "clearRecentTaskList, receiver is : "+receiver.toString());
} else {
Log.d(LOG_TAG, "clearRecentTaskList, receiver is NULL");
}
try {
recentTaskList = service.getDeclaredField("mRecentTasks");
recentTaskList.setAccessible(true);
Log.d(LOG_TAG, "clearRecentTaskList, recentTaskList gotton"+recentTaskList.toGenericString());
try {
systemRecentTask = recentTaskList.get(receiver);
} catch (IllegalArgumentException e1) {
Log.d(LOG_TAG, "IllegalArgumentException during try to clearRecentTask");
} catch (IllegalAccessException e1) {
Log.d(LOG_TAG, "IllegalAccessException during try to clearRecentTask");
}
Log.d(LOG_TAG, "Try to print the size of recent task: "+((ArrayList<T>) systemRecentTask).size());
} catch (SecurityException e) {
Log.d(LOG_TAG, "SecurityException during try to clearRecentTask");
} catch (NoSuchFieldException e) {
Log.d(LOG_TAG, "NoSuchFieldException during try to clearRecentTask");
}
}
使用此功能,我总是遇到“NullPointerException”,因为接收者是自己获得的空值。而且我尝试了另一种这样的方式(如果我删除所有的try/catch):
self = service.getDeclaredMethod("mSelf", null);
receiver = self.invoke(null, null); // mSelf is a static method which in ActivityMangerService class
同样的结果,我无法获取 ActivityManagerService 的实例,然后我无法获取 mRecentTasks。任何意见表示赞赏,虽然我不知道“如何删除此列表中的所有项目”,但这可能是另一个问题。
谢谢。