android.os.Process.killProcess(pid) did restart the processes again

断了今生、忘了曾经 提交于 2020-01-14 14:42:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!