私はランチャーを書いています。「最近のタスクリストにアプリが表示されませんでした」ではなく、システムから最近のアプリ/タスクリストをクリアする必要がありますが、今はわかりません。私は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");
}
}
この関数では、受信者が自分で取得したnullであるため、常に「NullPointerException」に遭遇します。そして、私はこのような別の方法を試しました(すべてのtry/catchを削除した場合):
self = service.getDeclaredMethod("mSelf", null);
receiver = self.invoke(null, null); // mSelf is a static method which in ActivityMangerService class
同じ結果、ActivityManagerService のインスタンスを取得できず、mRecentTasks を取得できません。「このリストのすべてのアイテムを削除する方法」はわかりませんが、コメントをお待ちしておりますが、別の質問になる可能性があります。
ありがとう。