Skip to content

Instantly share code, notes, and snippets.

@jcppkkk
Created August 8, 2012 08:33
Show Gist options
  • Save jcppkkk/3293436 to your computer and use it in GitHub Desktop.
Save jcppkkk/3293436 to your computer and use it in GitHub Desktop.
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