问题
How to kill an app in android?
killPackageProcesses(services.get(i).baseActivity.getPackageName());
this is the function 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);
}
It doesn't work. How does it work for other apps? The permission is written.
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
回答1:
You can't kill just any process in Android. From 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.
Also note that the permission you're requesting in your manifest doesn't work the way you think it does. According to the documentation, KILL_BACKGROUND_PROCESS:
Allows an application to call killBackgroundProcesses(String).
来源:https://stackoverflow.com/questions/18192502/how-to-kill-an-3rd-app-programmatically