问题
killprocess method does kill the processes but why the processes restart again and what is suppose to do for not restarting the process again. Here goes my code.
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> services = manager.getRunningAppProcesses();
for (RunningAppProcessInfo info : services) {
int service1name = info.pid;
android.os.Process.killProcess(service1name);
}
Thanks for your concern.
回答1:
Actually the process kill through the killProcess restart the process because android os assume that the process has been shutdown due to crashing and restart it again. What I find is we can use the higher priority am (activity manager) to kill the process. Example can be find here :
try {
Log.d("Application all process", "killing++");
String killCommand =
"am force-stop com.nil.android.ssd";
runAsRoot(new String[]{killCommand},true);
} catch (Exception e) {
e.printStackTrace();
}
public static void runAsRoot(String[] cmds, boolean shouldExit) {
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd + "\n");
}
if (shouldExit) {
os.writeBytes("exit\n");
}
os.flush();
} catch (Exception iOException) {
iOException.printStackTrace();
}
}
回答2:
I have faced the similar problem and It got solved with
this.finishAffinity();
And this is supported from API level 16.
来源:https://stackoverflow.com/questions/23234921/android-os-process-killprocesspid-did-restart-the-processes-again