I want to remove recent task list. Through googling I have found some links from stackoverflow. But none of them has been properly answered. Only one link
has showed a way using reflection. But now, I am facing a problem, android.permission.REMOVE_TASKS is denied. The code below is given, where you can see that in the try-catch clause, as the permission is denied,the code moves to catch() clause.
enter code here
private ActivityManager mActivityManager = null; private Method mRemoveTask;
public MyActivityManager(Context context) {
try {
Class<?> activityManagerClass = Class.forName("android.app.ActivityManager");
mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mRemoveTask = activityManagerClass.getMethod("removeTask", new Class[] { int.class, int.class });
mRemoveTask.setAccessible(true);
}
catch ( ClassNotFoundException e ) {
Log.i("MyActivityManager", "No Such Class Found Exception", e);
}
catch ( Exception e ) {
Log.i("MyActivityManager", "General Exception occurred", e);
}
}
/**
* If set, the process of the root activity of the task will be killed
* as part of removing the task.
*/
public static final int REMOVE_TASK_KILL_PROCESS = 0x0001;
/**
* Completely remove the given task.
*
* @param taskId Identifier of the task to be removed.
* @param flags Additional operational flags. May be 0 or
* {@link #REMOVE_TASK_KILL_PROCESS}.
* @return Returns true if the given task was found and removed.
*/
public boolean removeTask(int taskId, int flags) {
try {
return (Boolean) mRemoveTask.invoke(mActivityManager, Integer.valueOf(taskId), Integer.valueOf(flags) );
} catch (Exception ex) {
Log.i("MyActivityManager", "Task removal failed", ex);
}
return false;
}
public void clearRecentTasks() {
List<RecentTaskInfo> recents = mActivityManager.getRecentTasks(1000, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
// Start from 1, since we don't want to kill ourselves!
for( int i=1; i < recents.size(); i++ ) {
removeTask( recents.get(i).persistentId, 0);
}
}
}`
Here to be noted that, I have used this three permissions android.permission.GET_TASKS, android.permission.REORDER_TASKS, android.permission.REMOVE_TASKS some sources saying to kill background processes, but that will not remove the recent apps list. So I need some suggestions.