How can I kill a remote process for my app?

2020-07-26 10:31发布

问题:

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);
            }
        }
    }