Created
November 15, 2017 15:59
-
-
Save Hackforid/2c1a8acc1f7888b33cb184cd162dbeee to your computer and use it in GitHub Desktop.
[Android] Get Process Name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// High performance | |
public static String getProcessName() { | |
try { | |
File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline"); | |
BufferedReader mBufferedReader = new BufferedReader(new FileReader(file)); | |
String processName = mBufferedReader.readLine().trim(); | |
mBufferedReader.close(); | |
return processName; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
// Normal | |
public static String getProcessName(Context cxt, int pid) { | |
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE); | |
List<RunningAppProcessInfo> runningApps = am.getRunningAppProcesses(); | |
if (runningApps == null) { | |
return null; | |
} | |
for (RunningAppProcessInfo procInfo : runningApps) { | |
if (procInfo.pid == pid) { | |
return procInfo.processName; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment