Heath Borders
Heath Borders

Reputation: 32117

How to get pid of Activity configured to run in distinct process?

I have an Activity that needs to run in a separate process (which I have named :ChildWebView) to workaround an issue with WebViewDatabase. As part of the workaround, after the Activity is finished, I need to kill its process. Right now, I just call android.os.Process.myPid() in that activity and pass the result back to the parent main process inside an Intent. Is there a way for the parent main process to look up the pid of one of its child accessory processes by name?

Upvotes: 0

Views: 390

Answers (1)

Heath Borders
Heath Borders

Reputation: 32117

This didn't require any special permissions. It's pretty straightforward.

public void killProcessNamed(Context context, String processName) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList = activityManager.getRunningAppProcesses();
        if (runningAppProcessInfoList != null) {
            for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcessInfoList) {
                if (runningAppProcessInfo.processName.equals(processName)) {
                    android.os.Process.killProcess(runningAppProcessInfo.pid);
                    break;
                }
            }
        }
    }

Upvotes: 1

Related Questions