Created
July 11, 2016 21:12
-
-
Save akexorcist/e8ca80bc07ab2fa05cdee1f085b7a205 to your computer and use it in GitHub Desktop.
Get current running app name (Only supported on API 20 or lower)
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
| <uses-permission android:name="android.permission.GET_TASKS" /> |
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
| public static String getCurrentAppName(Context context) { | |
| ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
| ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0); | |
| String foregroundTaskPackageName = foregroundTaskInfo.topActivity.getPackageName(); | |
| PackageManager pm = context.getPackageManager(); | |
| try { | |
| PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0); | |
| return foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString(); | |
| } catch (PackageManager.NameNotFoundException e) { | |
| e.printStackTrace(); | |
| } | |
| return null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment