grep -irn “removeTask” frameworks/base/packages/SystemUI/
root@69959bbb90c6:/home/suhuazhi/9.1# grep -irn "removeTask" frameworks/base/packages/SystemUI/
frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/WorkLockActivityController.java:79: mIam.removeTask(taskId);
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java:755: ActivityManagerWrapper.getInstance().removeTask(event.task.key.id);
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java:1686: mStack.removeTask(t, AnimationProps.IMMEDIATE, false /* fromDockGesture */);
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java:1832: mStack.removeTask(event.task, event.animation, false /* fromDockGesture */);
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java:2108: Task removeTask = mStack.findTaskWithId(event.taskId);
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java:2109: if (removeTask != null) {
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java:2112: mStack.removeTask(removeTask, AnimationProps.IMMEDIATE, false /* fromDockGesture */,
frameworks/base/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java:623: getStack().removeTask(event.task, null, true /* fromDockGesture */);
frameworks/base/packages/SystemUI/tests/src/com/android/systemui/keyguard/WorkLockActivityControllerTest.java:96: verify(mIActivityManager, never()).removeTask(anyInt() /*taskId*/);
frameworks/base/packages/SystemUI/tests/src/com/android/systemui/keyguard/WorkLockActivityControllerTest.java:100: public void testRemoveTaskOnFailureToStartOverlay() throws Exception {
frameworks/base/packages/SystemUI/tests/src/com/android/systemui/keyguard/WorkLockActivityControllerTest.java:110: verify(mIActivityManager).removeTask(TASK_ID);
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/system/ActivityManagerWrapper.java:382: public void removeTask(int taskId) {
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/system/ActivityManagerWrapper.java:387: ActivityManager.getService().removeTask(taskId);
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/TaskResourceLoadQueue.java:47: void removeTask(Task t) {
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/RecentsTaskLoader.java:192: mLoadQueue.removeTask(t);
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/RecentsTaskLoader.java:198: mLoadQueue.removeTask(t);
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/TaskStack.java:82: public void removeTask(Task t, AnimationProps animation, boolean fromDockGesture) {
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/TaskStack.java:83: removeTask(t, animation, fromDockGesture, true /* dismissRecentsIfAllRemoved */);
frameworks/base/packages/SystemUI/shared/src/com/android/systemui/shared/recents/model/TaskStack.java:90: public void removeTask(Task t, AnimationProps animation, boolean fromDockGesture,
import android.app.ActivityManager;
public static final int NUM_TASKS_FOR_INSTANT_APP_INFO = 5;
// 获取最近任务列表
private Intent getTaskIntent(int taskId, int userId) {
try {
final List tasks =
ActivityManager.getService().getRecentTasks(
NUM_TASKS_FOR_INSTANT_APP_INFO, 0, userId).getList();
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i).id == taskId) {
return tasks.get(i).baseIntent;
}
}
} catch (RemoteException e) {
// Fall through
}
return null;
}
/**
* 清除最近任务 Removes a task by id.
*/
public void removeTask(int taskId) {
mBackgroundExecutor.submit(new Runnable() {
@Override
public void run() {
try {
ActivityManager.getService().removeTask(taskId);
} catch (RemoteException e) {
Log.w(TAG, "Failed to remove task=" + taskId, e);
}
}
});
}
需要 android.Manifest.permission.REMOVE_TASKS 权限,使用如下getRecentTasks获取最近运行列表,ActivityManager.removeTask()移除应用
/**
* android.Manifest.permission.REMOVE_TASKS
*
* @param mContext
*/
public static void removeRecentTask(Context mContext) {
ActivityManager mAm = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List task = mAm.getRecentTasks(21, ActivityManager.RECENT_IGNORE_UNAVAILABLE | ActivityManager.RECENT_INCLUDE_PROFILES);
int size = task == null ? 0 : task.size();
for (int i = 0; i < size; i++) {
// 清除最近任务
mAm.removeTask(info.id);
}
frameworks/base/core/java/android/app/ActivityManager.java
root@69959bbb90c6:/home/suhuazhi/9.1# find frameworks/ -name "ActivityManager.java"
frameworks/base/core/java/android/app/ActivityManager.java
package android.app;
/**
* @hide
*/
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}
private static final Singleton IActivityManagerSingleton =
new Singleton() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};
该接口Android P 版本仍旧可以继续使用,因为最终也是调用getService().getRecentTasks
package android.app;
@SystemService(Context.ACTIVITY_SERVICE)
public class ActivityManager {
/**
* Return a list of the tasks that the user has recently launched, with
* the most recent being first and older ones after in order.
*
* Note: this method is only intended for debugging and presenting
* task management user interfaces. This should never be used for
* core logic in an application, such as deciding between different
* behaviors based on the information found here. Such uses are
* not supported, and will likely break in the future. For
* example, if multiple applications can be actively running at the
* same time, assumptions made about the meaning of the data here for
* purposes of control flow will be incorrect.
*
* @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method is
* no longer available to third party applications: the introduction of
* document-centric recents means
* it can leak personal information to the caller. For backwards compatibility,
* it will still return a small subset of its data: at least the caller's
* own tasks (though see {@link #getAppTasks()} for the correct supported
* way to retrieve that information), and possibly some other tasks
* such as home that are known to not be sensitive.
*
* @param maxNum The maximum number of entries to return in the list. The
* actual number returned may be smaller, depending on how many tasks the
* user has started and the maximum number the system can remember.
* @param flags Information about what to return. May be any combination
* of {@link #RECENT_WITH_EXCLUDED} and {@link #RECENT_IGNORE_UNAVAILABLE}.
*
* @return Returns a list of RecentTaskInfo records describing each of
* the recent tasks.
*/
@Deprecated
public List getRecentTasks(int maxNum, int flags)
throws SecurityException {
try {
if (maxNum < 0) {
throw new IllegalArgumentException("The requested number of tasks should be >= 0");
}
return getService().getRecentTasks(maxNum, flags, mContext.getUserId()).getList();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
root@69959bbb90c6:/home/suhuazhi/9.1# find frameworks/ -name "ActivityManagerService.java"
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
这里需要 android.Manifest.permission.GET_DETAILED_TASKS 权限
@Override
public ParceledListSlice getRecentTasks(int maxNum, int flags,
int userId) {
final int callingUid = Binder.getCallingUid();
userId = mUserController.handleIncomingUser(Binder.getCallingPid(), callingUid, userId,
false, ALLOW_FULL_ONLY, "getRecentTasks", null);
final boolean allowed = isGetTasksAllowed("getRecentTasks", Binder.getCallingPid(),
callingUid);
final boolean detailed = checkCallingPermission(
android.Manifest.permission.GET_DETAILED_TASKS)
== PackageManager.PERMISSION_GRANTED;
synchronized (this) {
return mRecentTasks.getRecentTasks(maxNum, flags, allowed, detailed, userId,
callingUid);
}
}
@Override
public boolean removeTask(int taskId) {
enforceCallerIsRecentsOrHasPermission(REMOVE_TASKS, "removeTask()");
synchronized (this) {
final long ident = Binder.clearCallingIdentity();
try {
return mStackSupervisor.removeTaskByIdLocked(taskId, true, REMOVE_FROM_RECENTS,
"remove-task");
} finally {
Binder.restoreCallingIdentity(ident);
}
}
}
/**
* This can be called with or without the global lock held.
*/
void enforceCallerIsRecentsOrHasPermission(String permission, String func) {
if (!mRecentTasks.isCallerRecents(Binder.getCallingUid())) {
enforceCallingPermission(permission, func);
}
}
上述需要需要 import static android.Manifest.permission.REMOVE_TASKS;的权限
import android.app.ActivityManager;
/**
* [Android P]Removes a task by id.
*/
public static void removeTask(int taskId) {
try {
ActivityManager.getService().removeTask(taskId);
} catch (RemoteException e) {
Log.w(TAG, "Failed to remove task=" + taskId, e);
}
}
对应权限