Alternative to getRunningTasks in Android L
Asked Answered
B

1

26

It's been well documented that getRunningTasks is now deprecated in Android L. I used this to get top activity and this was an important feature of my app. Though not complete, a new solution in Android L has been promised by a Google Employee. The solution states:

"for L we do plan to have a new solution that will address some of the existing use cases of getRecentTasks(): we hope to expose the system’s internal usage stats service in a new and expanded form that is available to third party apps through the SDK.

The new usage stats service should have two major pieces of information. First, it will provided aggregated stats of the time the user spent in each app, and the last launch time of those apps, rolled up over days, weeks, months, and years. Second, it will include detailed usage events over the last few days, describing the time and ComponentName when any activity transitions to the foreground or background.

Access to this new usage data will require that the user explicitly grant the app access to usage stats through a new settings UI. They will be able to see all apps that have access and revoke access at any point.

I realize that this won’t cover every possible use of the old getRecentsTask API, and I apologize for that. However, for what we see to be the key current uses of the API, a new usage stats API should be much more robust and efficient."

You can read more about it here: https://code.google.com/p/android-developer-preview/issues/detail?id=29#c50

I have tried looking in the Android L documentation but cannot find anything that talks the usage stats service mentioned above. Has anyone used this API? Can anyone point me to the documentation for this?

Blamed answered 16/10, 2014 at 9:10 Comment(2)
"I used this to get top activity" . Do you mean to the top Activity of your app ? or the top activity of any app ?Yodle
Please see my answer in another thread https://mcmap.net/q/89543/-getrunningtasks-doesn-39-t-work-in-android-lWithdrawal
Y
21

The system statistics api evoked in your question is also presented here and the detailed API is here. As far as I can see this API won't allow you to get the current top activity.

i.e. you have the ability to receive UsageEvent with the type MOVE_TO_FOREGROUND by using UsageStatsManager.queryEvents but according javadoc :

The last few minutes of the event log will be truncated to prevent abuse by applications.

So it is really an API to get statistics, not to get realtime information. (note that the now deprecated getRunningTasks() was neither intended to retrieve realtime data : javadoc)

On the other hand, if you are only interested in top activity of your own app: you can use getAppTasks() returning a List<AppTask> where you can get the RecentTaskInfo with getTaskInfo


EDIT : another approach is provided here.

It is based on UsageStats and UsageStats.getLastTimeUsed(). Drawback : it requires additional permission and gives you only the package name -not the activity-, but it gives you real time data.

Yodle answered 19/10, 2014 at 13:36 Comment(6)
im implemeting some kind of app-pinlock, so, from this info - to me - i cannot get any valid information if the application was left, an re-started, right? or u got possibly any hints?Alejandraalejandrina
You're right about the documentation saying queryEvents has the last few minutes truncated, but queryUsageStats doesn't have that problem and can be used for a similar purpose. See this answer for an example.Urethroscope
@Urethroscope the other answer gives you UsageStats (i.e. aggregate data) and not UsageEvents and additionally it requires another permission. IMO both solutions are not- perfect-alternatives to deprecated getRunningTasks() (if you need tasks info from third-party apps)Yodle
@ben75, but at least it's an option. Your answer implies you can't get the top activity in real-time, but the code I linked to seems to do exactly that. Yes, you require a permission, but that doesn't make the API inappropriate to use altogether.Urethroscope
@ben75, is your point about the aggregate data that the queryUsageStatus approach isn't going to be completely accurate?Urethroscope
I missed getLastTimeStamp() which is indeed providing useful data to get real time info. On the other hand, UsageStats gives you only the packageName while UsageEvent gives you also the Activity thanks to the getClassname() method.Yodle

© 2022 - 2024 — McMap. All rights reserved.