如何为我的应用程序终止远程进程?

时间:2015-08-04 03:45:08

标签: java android activity-manager remote-process

我们的应用程序使用此方法来终止远程进程;但是可以使用任何其他计划吗?我需要这个,因为getRunningAppProcesses()可能不起作用。

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 个答案:

答案 0 :(得分:0)

根据@Matt Clark的建议你可以这样做

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

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

答案 1 :(得分:0)

检查进程名称是否包含:remote,然后继续。

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