{
	"id": "bde8a6b2-c90f-4cac-b760-003ff076e1db",
	"created_at": "2026-04-06T00:15:43.0734Z",
	"updated_at": "2026-04-10T03:20:27.048334Z",
	"deleted_at": null,
	"sha1_hash": "bd3ea4a253a6c84506dc3839e3b7d55e27802e36",
	"title": "Android 5.1.1 and above - getRunningAppProcesses() returns my application package only",
	"llm_title": "",
	"authors": "",
	"file_creation_date": "0001-01-01T00:00:00Z",
	"file_modification_date": "0001-01-01T00:00:00Z",
	"file_size": 144197,
	"plain_text": "Android 5.1.1 and above - getRunningAppProcesses() returns my\r\napplication package only\r\nBy Lior Iluz 26.6k1616 gold badges6868 silver badges116116 bronze badges\r\nPublished: 2015-06-03 · Archived: 2026-04-05 13:33:21 UTC\r\nIt seems Google finally closed all doors for getting the current foreground application package.\r\nAfter the Lollipop update, which killed getRunningTasks(int maxNum) and thanks to this answer, I used this\r\ncode to get the foreground application package since Lollipop:\r\nfinal int PROCESS_STATE_TOP = 2;\r\nRunningAppProcessInfo currentInfo = null;\r\nField field = null;\r\ntry {\r\n field = RunningAppProcessInfo.class.getDeclaredField(\"processState\");\r\n} catch (Exception ignored) {\r\n}\r\nActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);\r\nList\u003cRunningAppProcessInfo\u003e appList = am.getRunningAppProcesses();\r\nfor (RunningAppProcessInfo app : appList) {\r\n if (app.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND \u0026\u0026\r\n app.importanceReasonCode == 0 ) {\r\n Integer state = null;\r\n try {\r\n state = field.getInt( app );\r\n } catch (Exception ignored) {\r\n }\r\n if (state != null \u0026\u0026 state == PROCESS_STATE_TOP) {\r\n currentInfo = app;\r\n break;\r\n }\r\n }\r\n}\r\nreturn currentInfo;\r\nAndroid 5.1.1 and above (6.0 Marshmallow), it seems, killed getRunningAppProcesses() as well. It now returns\r\na list of your own application package.\r\nUsageStatsManager\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 1 of 11\n\nWe can use the new UsageStatsManager API as described here but it doesn't work for all applications. Some\r\nsystem applications will return the same package\r\ncom.google.android.googlequicksearchbox\r\nAccessibilityService (December 2017: Going to be banned for use by Google)\r\nSome applications use AccessibilityService (as seen here) but it has some disadvantages.\r\nIs there another way of getting the current running application package?\r\nasked Jun 3, 2015 at 11:52\r\n18\r\nTo get a list of running processes on Android 1.6 - Android 6.0 you can use this library I wrote:\r\nhttps://github.com/jaredrummler/AndroidProcesses The library reads /proc to get process info.\r\nGoogle has significantly restricted access to /proc in Android Nougat. To get a list of running processes on\r\nAndroid Nougat you will need to use UsageStatsManager or have root access.\r\nClick the edit history for previous alternative solutions.\r\nanswered Sep 3, 2015 at 3:27\r\nJared Rummler\r\n38.2k21 gold badges137 silver badges149 bronze badges\r\n32 Comments\r\n@androiddeveloper no, I only used libsuperuser because it provides an easy way to run a shell command (non-root or root) and get the output. I can re-write it without libsuperuser if there is enough demand.\r\n2015-09-11T14:57:36.97Z+00:00\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 2 of 11\n\nSorry for that. Was just being curious. :(\r\n2015-09-12T16:37:21.64Z+00:00\r\n@JaredRummler I'm currently in the process of implementing your solution into my app. Seems to be working\r\nfine as far as I can tell\r\n2015-09-21T06:03:08.15Z+00:00\r\n@androiddeveloper, If you want to sort it based on a different attribute, make Process implement Comparable\r\nand override the compareTo method. Then, when you use Collections.sort(processesList) , it will use the\r\norder you specified.\r\n2015-10-11T18:42:39.597Z+00:00\r\n2015-10-21T17:40:30.96Z+00:00\r\n private String printForegroundTask() {\r\n String currentApp = \"NULL\";\r\n if(android.os.Build.VERSION.SDK_INT \u003e= android.os.Build.VERSION_CODES.LOLLIPOP) {\r\n UsageStatsManager usm = (UsageStatsManager)this.getSystemService(\"usagestats\");\r\n long time = System.currentTimeMillis();\r\n List\u003cUsageStats\u003e appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time\r\n if (appList != null \u0026\u0026 appList.size() \u003e 0) {\r\n SortedMap\u003cLong, UsageStats\u003e mySortedMap = new TreeMap\u003cLong, UsageStats\u003e();\r\n for (UsageStats usageStats : appList) {\r\n mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);\r\n }\r\n if (mySortedMap != null \u0026\u0026 !mySortedMap.isEmpty()) {\r\n currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();\r\n }\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 3 of 11\n\n}\r\n } else {\r\n ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);\r\n List\u003cActivityManager.RunningAppProcessInfo\u003e tasks = am.getRunningAppProcesses();\r\n currentApp = tasks.get(0).processName;\r\n }\r\n Log.e(\"adapter\", \"Current App in foreground is: \" + currentApp);\r\n return currentApp;\r\n}\r\nUse this method for getting foreground task. U will need an System Permission \"android:get_usage_stats\"\r\npublic static boolean needPermissionForBlocking(Context context){\r\n try {\r\n PackageManager packageManager = context.getPackageManager();\r\n ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);\r\n AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);\r\n int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applic\r\n return (mode != AppOpsManager.MODE_ALLOWED);\r\n } catch (PackageManager.NameNotFoundException e) {\r\n return true;\r\n }\r\n}\r\nIF user enable this in setting -\u003e Security-\u003e app with usage access. After that u will get foreground task. Similar\r\nprocess Clean matser by Cheetahamobile google play link\r\nanswered Sep 1, 2015 at 11:28\r\nTarun Sharma\r\n8321 gold badge11 silver badges24 bronze badges\r\n11 Comments\r\nAs pointed out in the OP and the comments, there are major downsides of using UsageStatsManager . Samsung\r\nhas removed requests and it is quite annoying for a user to grant a system permission.\r\n2015-09-01T11:32:48.363Z+00:00\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 4 of 11\n\nI have tested in moto e2 device. It is working fine and yes, we need to implement this permission. we all facing\r\nsame issues. We all really need a better approach. Good luck dude\r\n2015-09-01T12:02:24.72Z+00:00\r\nThis approach does not work at least on LG G3 because there is no \"Settings -\u003e Security-\u003e App with usage\r\naccess\" menu item\r\n2015-09-01T14:45:21.217Z+00:00\r\nThat's not an answer to my question. Moreover, no new info is provided in this answer.\r\n2015-09-05T06:55:14.947Z+00:00\r\nWorking fine but not properly in marshmallow. if notification came then current running process will be the\r\npackage notification received. actually app is not running in foreground or background :(. need solution.\r\n2016-07-01T12:01:01.607Z+00:00\r\nTake a look at https://github.com/ricvalerio/foregroundappchecker, it might be what you need. Provides sample\r\ncode, and takes away the pain of having to implement cross version foreground detector.\r\nHere are two samples:\r\nAppChecker appChecker = new AppChecker();\r\nString packageName = appChecker.getForegroundApp();\r\nOr regularly check:\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 5 of 11\n\nAppChecker appChecker = new AppChecker();\r\nappChecker\r\n .when(\"com.other.app\", new AppChecker.Listener() {\r\n @Override\r\n public void onForeground(String packageName) {\r\n // do something\r\n }\r\n )\r\n .when(\"com.my.app\", new AppChecker.Listener() {\r\n @Override\r\n public void onForeground(String packageName) {\r\n // do something\r\n }\r\n )\r\n .other(new AppChecker.Listener() {\r\n @Override\r\n public void onForeground(String packageName) {\r\n // do something\r\n }\r\n )\r\n .timeout(1000)\r\n .start(this);\r\nanswered Aug 28, 2016 at 23:06\r\nrvalerio\r\n4975 silver badges5 bronze badges\r\n2 Comments\r\nThanks but nothing's new here. He just wrapped UsageStatsManager API which is mentioned in OP. The user will\r\nneed to enable access, as other methods since Android 5.1.1\r\n2016-08-29T04:52:17.22Z+00:00\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 6 of 11\n\n@Jérémy did you request the required permissions?\r\n2019-01-13T19:26:35.827Z+00:00\r\nGoogle limited this functionality for system apps only. As been reported in a bug ticket, you will need the\r\nREAL_GET_TASKS permission to access there.\r\nApplications must now have ...permission.REAL_GET_TASKS to be able to get process information\r\nfor all applications. Only the process information for the calling application will be returned if the app\r\ndoesn't have the permission. Privileges apps will temporarily be able to get process information for all\r\napplications if they don't have the new permission, but have deprecated ...permission.GET_TASKS\r\nAlso,only system apps can acquire the REAL_GET_TASKS permission.\r\nanswered Sep 2, 2015 at 15:09\r\nIlya Gazman\r\n32.6k25 gold badges152 silver badges239 bronze badges\r\n2 Comments\r\nI saw applock is still working on 5.1.1, once the app get the permission in security-\u003e\"apps with usage access\"...\r\nThis is somewhat surprising\r\n2015-09-22T01:06:49.647Z+00:00\r\n\"Permissions with the protection level signature, privileged or signatureOrSystem are only granted to system apps.\r\nIf an app is a regular non-system app, it will never be able to use these permissions. \" say android studio.. Good\r\nluck to negociate with Google to be accepted as a \"system app\".\r\n2019-01-12T18:55:25.35Z+00:00\r\nJust throwing out a potential optimization to what I imagine is a heavily copy-pasted bit of code for detecting the\r\ntop-most application on Android M.\r\nThis\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 7 of 11\n\nif (android.os.Build.VERSION.SDK_INT \u003e= android.os.Build.VERSION_CODES.LOLLIPOP) {\r\n UsageStatsManager usm = (UsageStatsManager)this.getSystemService(\"usagestats\");\r\n long time = System.currentTimeMillis();\r\n List\u003cUsageStats\u003e appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time);\r\n if (appList != null \u0026\u0026 appList.size() \u003e 0) {\r\n SortedMap\u003cLong, UsageStats\u003e mySortedMap = new TreeMap\u003cLong, UsageStats\u003e();\r\n for (UsageStats usageStats : appList) {\r\n mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);\r\n }\r\n if (mySortedMap != null \u0026\u0026 !mySortedMap.isEmpty()) {\r\n currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();\r\n }\r\n }\r\n}\r\nCan be simplified to this\r\nif (android.os.Build.VERSION.SDK_INT \u003e= android.os.Build.VERSION_CODES.LOLLIPOP) {\r\n UsageStatsManager usm = (UsageStatsManager) context.getSystemService(\r\n Context.USAGE_STATS_SERVICE);\r\n long time = System.currentTimeMillis();\r\n List\u003cUsageStats\u003e appStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,\r\n time - 1000 * 1000, time);\r\n if (appStatsList != null \u0026\u0026 !appStatsList.isEmpty()) {\r\n currentApp = Collections.max(appStatsList, (o1, o2) -\u003e\r\n Long.compare(o1.getLastTimeUsed(), o2.getLastTimeUsed())).getPackageName();\r\n }\r\n}\r\nI found myself using this code in a 2 second loop, and wondered why I was using a complex solution that was\r\nO(n*log(n)) when a more simple solution was available in Collections.max() which is O(n).\r\nanswered Dec 1, 2017 at 1:07\r\nbstar55\r\n3,6643 gold badges23 silver badges25 bronze badges\r\nComments\r\npublic class AccessibilityDetectingService extends AccessibilityService {\r\n@Override\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 8 of 11\n\nprotected void onServiceConnected() {\n super.onServiceConnected();\n //Configure these here for compatibility with API 13 and below.\n AccessibilityServiceInfo config = new AccessibilityServiceInfo();\n config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;\n config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;\n if (Build.VERSION.SDK_INT \u003e= 16)\n //Just in case this helps\n config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;\n setServiceInfo(config);\n}\n@Override\npublic void onAccessibilityEvent(final AccessibilityEvent event) {\n if (event == null ) {\n return;\n } else if(event.getPackageName() == null \u0026\u0026 event.getClassName() == null){\n return;\n }\n if (activityInfo != null){\n Log.d(\"CurrentActivity\", componentName.flattenToShortString());\n }\n}\nprivate ActivityInfo tryGetActivity(ComponentName componentName) {\n try {\n return getPackageManager().getActivityInfo(componentName, 0);\n } catch (PackageManager.NameNotFoundException e) {\n return null;\n }\n}\n@Override\npublic void onInterrupt() {\n}\n}\n}//`enter code here`uses-permission android:name=\"android.permission.BIND_ACCESSIBILITY_SERVICE\" /\u003e\nThen start the service and app accessibility on in your device setting-\u003eaccessibility-\u003eApp on that service.\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\nPage 9 of 11\n\nanswered Feb 2, 2016 at 11:22\r\n1 Comment\r\nThis is already covered in the question, and you've just copy-pasted the code from its original source on\r\nStackOverflow.\r\n2019-02-17T10:16:05.827Z+00:00\r\nPlease try to use getRunningServices() instead of getRunningAppProcesses() method.\r\n ActivityManager mActivityManager = (ActivityManager) getSy stemService(Context.ACTIVITY_SERVICE);\r\n List\u003cActivityManager.RunningServiceInfo\u003e appProcessInfoList = mActivityManager.getRunningServices(Integer.MAX_V\r\nsoumya\r\n3,8019 gold badges39 silver badges70 bronze badges\r\nanswered Oct 9, 2015 at 9:23\r\n1 Comment\r\nWelcome to Stack Overflow! I don't think this will work since the foreground app might not be running a service.\r\n2015-10-23T21:05:31.85Z+00:00\r\nStart asking to get answers\r\nFind the answer to your question by asking.\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 10 of 11\n\nAsk question\r\nExplore related questions\r\nSee similar questions with these tags.\r\nSource: http://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nhttp://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag\r\nPage 11 of 11",
	"extraction_quality": 1,
	"language": "EN",
	"sources": [
		"MITRE"
	],
	"references": [
		"http://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag"
	],
	"report_names": [
		"android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag"
	],
	"threat_actors": [],
	"ts_created_at": 1775434543,
	"ts_updated_at": 1775791227,
	"ts_creation_date": 0,
	"ts_modification_date": 0,
	"files": {
		"pdf": "https://archive.orkl.eu/bd3ea4a253a6c84506dc3839e3b7d55e27802e36.pdf",
		"text": "https://archive.orkl.eu/bd3ea4a253a6c84506dc3839e3b7d55e27802e36.txt",
		"img": "https://archive.orkl.eu/bd3ea4a253a6c84506dc3839e3b7d55e27802e36.jpg"
	}
}