In Android L, Google has disabled getRunningTasks. Now it can only return own apps task and the home launcher. I can no longer get other apps tasks. Our app needs that method to determine current top app. Any one has another method to do this?
I have searched in Google, no more topics about this except this: https://code.google.com/p/android-developer-preview/issues/detail?id=29
For a recent project that I worked on, I also need to detect when certain applications are launched. All my research lead to the getRunningTasks method, which is deprecated starting from Lollipop. However, to my surprises, I discovered that some of the app lock apps still work on lollipop devices, so they must have come up with a solution to get around this. So I dug a little deeper. Here is what I found out:
Some sample code to detect when the default calendar app is launched:
Note: getRunningAppProcesses is also intended for debugging or "building a user-facing process management UI". Not sure if google will close this backdoor the similar way they did to getRunningTasks.
So no, you can't get the topActivity anymore. But with a little bit hack you can achieve similar result.
Here's an exact solution to get current top activity on your Android L/Lollipop devices and Android M/Marshmallow devices.
First call this line of code:(One time)
The above code will open a screen named "Apps with usage access". Just check the radio button to on/true to allow usage access. Now call the following method in your service or anywhere you want:
add permission
This will return the package name of currently running activity , whether it is facebook or whatsapp.
The only complication of this method is you need to prompt user for allowing app usage stats ... i.e. the first step.
Hope! this helps everyone.
As MKY mentioned,
getRunningTasks()
method does not work for getting the current application in Lollipop.As sunxin8086 wrote, the one way for getting the running applications is by using
getRunningAppsProcesses()
method. However, the conditioninfo.importance == IMPORTANCE_FOREGROUND
can not determine the current app uniquely.The better approach to determine the current foreground application may be checking the
processState
field inRunningAppProcessInfo
object. This field is a hidden field, but you can see it in theRunningAppProcessInfo
class. If this value isActivityManager.PROCESS_STATE_TOP
(which is also hidden static constant), the process is the current foreground process.For example the code is
Note:
processState
field does not exist in pre-Lolipop. Please check thatBuild.VERSION.SDK_INT >= 21
before running the above code. The above code works only for Lollipop+.The other approach, by Gaston (which is quite different), and the meaning of 'current application' is slightly different from this approach.
Please choose one for your purpose.
[EDIT]
As Sam pointed out, I modified
START_TASK_TO_FRONT
byPROCESS_STATE_TOP
. (Both values are 2)[EDIT2]
Sam has a new find! To determine the foreground application uniquely, one more condition
is necessary. The above code has been updated. Thanks!
I Think its not possible to get other app's tasks,
This is what documentation says
Check out the api overview of Android L here https://developer.android.com/preview/api-overview.html#Behaviors