whether this process is named with process Name - Android Android OS

Android examples for Android OS:Process

Description

whether this process is named with process Name

Demo Code


//package com.java2s;

import android.app.ActivityManager;

import android.content.Context;

import android.text.TextUtils;

import java.util.List;

public class Main {
    /**/*from  w w w  . j  ava 2s . com*/
     * whether this process is named with processName
     */
    public static boolean isNamedProcess(Context context, String processName) {
        if (context == null || TextUtils.isEmpty(processName)) {
            return false;
        }

        int pid = android.os.Process.myPid();
        ActivityManager manager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfoList = manager
                .getRunningAppProcesses();
        if (processInfoList != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
                if (processInfo.pid == pid
                        && processName
                                .equalsIgnoreCase(processInfo.processName)) {
                    return true;
                }
            }
        }
        return false;
    }
}

Related Tutorials