我想删除最近的任务列表。通过谷歌搜索我发现了stackoverflow的一些链接。但是没有一个得到适当的回答。只有一个链接
展示了一种使用反射的方法。但是现在,我遇到了一个问题,android.permission.REMOVE_TASKS被拒绝了。给出了下面的代码,您可以在try-catch子句中看到,当权限被拒绝时,代码将移至catch()子句。
enter code here
私人ActivityManager mActivityManager = null; private方法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);
}
}
}`
需要注意的是,我已经使用了这三个权限android.permission.GET_TASKS,android.permission.REORDER_TASKS,android.permission.REMOVE_TASKS 一些消息来源说杀死后台进程,但这不会删除最近的应用程序列表。 所以我需要一些建议。
答案 0 :(得分:0)
android.permission.REMOVE_TASKS被拒绝
该权限是signature
级权限。如果您的应用使用与设备固件相同的签名密钥进行签名,则只能使用它。