Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get topTask and it's PID
static final String TAG = "XXX_TAG"
am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
RunningTaskInfo topTask = am.getRunningTasks(1).get(0);
String pkg = topTask.baseActivity.getPackageName();
int pid = findForgroundPID(pkg);
private int findForgroundPID(String topPackage) {
int pid = -1;
List<RunningAppProcessInfo> procList = am.getRunningAppProcesses();
for (RunningAppProcessInfo proc : procList) {
if (proc.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String pkg : proc.pkgList) {
if (pkg.equals(topPackage)) {
pid = proc.pid;
Log.v(TAG, "[last app]" + topPackage + " [PID]" + pid );
}
}
}
}
return pid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.