gpt4 book ai didi

安卓任务 killer

转载 作者:太空狗 更新时间:2023-10-29 12:52:38 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的任务 killer 。我知道我不应该在 Android 中终止任务,但我很想尝试这样的事情。我有以下代码:

List<RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();
for (int i = 0; i < procInfo.size(); i++) {
Log.v("proces " + i,procInfo.get(i).processName + " pid:" + procInfo.get(i).pid + " importance: " + procInfo.get(i).importance + " reason: " + procInfo.get(i).importanceReasonCode);
//First I display all processes into the log
}
for (int i = 0; i < procInfo.size(); i++) {
RunningAppProcessInfo process = procInfo.get(i);
int importance = process.importance;
int pid = process.pid;
String name = process.processName;
if (name.equals("manager.main")) {
//I dont want to kill this application
continue;
}
if (importance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {
//From what I have read about importances at android developers, I asume that I can safely kill everithing except for services, am I right?
Log.v("manager","task " + name + " pid: " + pid + " has importance: " + importance + " WILL NOT KILL");
continue;
}
Log.v("manager","task " + name + " pid: " + pid + " has importance: " + importance + " WILL KILL");
android.os.Process.killProcess(procInfo.get(i).pid);
}
procInfo = activityManager.getRunningAppProcesses();
//I get a new list with running tasks
for (int i = 0; i < procInfo.size(); i++) {
Log.v("proces after killings" + i,procInfo.get(i).processName + " pid:" + procInfo.get(i).pid + " importance: " + procInfo.get(i).importance + " reason: " + procInfo.get(i).importanceReasonCode);
}

我这里的问题是,当我执行这段代码时,我首先得到所有任务的列表,没关系。比我在日志中看到的几行:

Sending signal. pid: (processId) SIG: 9

我认为那是死亡的信号。但是在我的代码末尾,当我显示所有正在运行的进程时,列表和以前一样,没有任务被杀死。知道为什么吗?谢谢!

最佳答案

您不能以这种方式终止其他任务,因为内核会强制执行权限。试试这个:

ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.restartPackage(packageName);

您需要 list 中的以下权限:

<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

编辑:

实际上 restartPackage 已被弃用。请改用 killBackgroundProcesses()!

关于安卓任务 killer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471239/

25 4 0
文章推荐: javascript - html5和Javascript会取代原生应用吗?
文章推荐: git - 我的 git push 卡在了一个大文件上。该怎么办?
文章推荐: git - 通过 Sublime text 管理 Git 或 bitbucket 存储库
文章推荐: javascript - html标签使用/替换