How can I kill a remote process for my app?

浪子不回头ぞ 提交于 2020-06-25 05:59:09

问题


Our app uses this method to kill remote process; but can any other plan can be used? I need this because getRunningAppProcesses() may not work.

public void killChildProcess() {
    ActivityManager actvityManager = (ActivityManager)this.getSystemService( Context.ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

    for(RunningAppProcessInfo procInfo : procInfos) {
        if (procInfo.processName != null && 
                procInfo.processName.equals("package:remote")) {

            Process.killProcess(procInfo.pid);
        }
    }
}

回答1:


As suggested by @Matt Clark you can do like this

Long PID=Runtime.getRuntime()
  .exec("ps -ef|grep 'package:remote'| awk '{ if(NR==2) print ($2)}'")

Runtime.getRuntime.exec("kill -9 "+PID);



回答2:


Check the process name contains :remote then go ahead.

    @Override
    public void killRemoteProcess() {
        ActivityManager activityManager =  (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();

        for (ActivityManager.RunningAppProcessInfo appProcessInfo: runningAppProcesses) {
            if (appProcessInfo.processName.contains(getApplicationInfo().processName + ":remote")) {
                Log.d("Killing active process: " + getApplicationInfo().processName + ":remote");
                android.os.Process.killProcess(appProcessInfo.pid);
            }
        }
    }


来源:https://stackoverflow.com/questions/31800316/how-can-i-kill-a-remote-process-for-my-app

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