Created
August 8, 2012 08:33
-
-
Save jcppkkk/3293436 to your computer and use it in GitHub Desktop.
Get topTask and it's PID
This file contains 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
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