get Process Name, null may be returned if the specified process not found - Android Android OS

Android examples for Android OS:Process

Description

get Process Name, null may be returned if the specified process not found

Demo Code

import android.app.ActivityManager;
import android.content.Context;
import java.util.List;

public class Main{

    /**/*from www.java2  s . com*/
     * @return null may be returned if the specified process not found
     */
    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am
                .getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }

}

Related Tutorials