How can I kill a remote process for my app?

2020-07-26 10:29发布

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

2条回答
We Are One
2楼-- · 2020-07-26 11:00

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);
做个烂人
3楼-- · 2020-07-26 11:16

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);
            }
        }
    }
查看更多
登录 后发表回答