gpt4 book ai didi

Android,如何清除大多数手机中可以从主页按钮获得的最近任务列表?反射(reflection)是一种可能的方式吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:23 24 4
gpt4 key购买 nike

我正在写一个启动器,它需要从系统中清除最近的应用程序/任务列表,而不是“没有在最近的任务列表中显示我的应用程序”,但我现在对此一无所知。我在 stackoverflow 中搜索过,只有 this issue匹配但答案没有任何帮助。其他人也问过同样的问题,他提到了来自 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”,因为接收者是自己得到的null。我已经尝试过另一种方式(如果我删除所有 try/catch):

self = service.getDeclaredMethod("mSelf", null);
receiver = self.invoke(null, null); // mSelf is a static method which in ActivityMangerService class

同样的结果,获取不到ActivityManagerService的实例,然后获取不到mRecentTasks。任何意见表示赞赏,虽然我不知道“如何删除此列表中的所有项目”,但这可能是另一个问题。

谢谢。

最佳答案

虽然我可以编辑我之前的答案,但我在不同项目中使用的这个解决方案完全不同(而且更容易),所以最好重新开始......

在这种情况下,我只是使用反射来访问 ActivityManager 的 removeTask() 方法,并将所有这些包装到 MyActivityManager 的便利类中,如下所示:

import java.lang.reflect.Method;

public class MyActivityManager {


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);
}
}

}

然后,当我想清除最近的任务时,我会执行以下操作:

new MyActivityManager(this).clearRecentTasks();

这可能需要其中一个或两个才能起作用:

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

在我的例子中,应用程序是使用系统权限安装的。

关于Android,如何清除大多数手机中可以从主页按钮获得的最近任务列表?反射(reflection)是一种可能的方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12890415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com