Skip to content

Instantly share code, notes, and snippets.

@Hackforid
Created November 15, 2017 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hackforid/2c1a8acc1f7888b33cb184cd162dbeee to your computer and use it in GitHub Desktop.
Save Hackforid/2c1a8acc1f7888b33cb184cd162dbeee to your computer and use it in GitHub Desktop.
[Android] Get Process Name
// 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