如何杀死 Android 中的应用程序?
killPackageProcesses(services.get(i).baseActivity.getPackageName());
这是函数killPackageProcesses
public void killPackageProcesses(String packagename) {
int pid = 0;
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am
.getRunningAppProcesses();
for (int i = 0; i < pids.size(); i++) {
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equalsIgnoreCase(packagename)) {
pid = info.pid;
}
}
android.os.Process.killProcess(pid);
}
这不起作用。它如何适用于其他应用程序? 许可已写。
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
最佳答案
您不能随意终止 Android 中的任何进程。来自 the documentation on Process
:
Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.
另请注意,您在 list 中请求的权限并不按您想象的方式工作。根据documentation ,KILL_BACKGROUND_PROCESS
:
Allows an application to call killBackgroundProcesses(String).
关于java - 如何以编程方式杀死第三个应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192502/