Android UsageStatsManager sample

Google has deprecated the function “getRecentTasks” of “ActivityManager” class. Now all it does is to get the list of apps that the current app has opened. But if you are writing app that “locks” access to specific apps with password or any other protection methods, you still need to be able to get top most activity/package to react accordingly to user changing current app and for example show your overlay.

Google introduces UsageStatsManager in api 21, which can replace “getRecentTasks” for new versions.

I’ve created sample project demonstrating how to use this api.

First of all you need to declare permission in AndroidManifest.xml

Adding

tools:ignore="ProtectedPermissions"

will disable warning that permission is system level and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.

So we need to check if our app was granted this permission

and if no, ask user to enable it

We are launching settings activity to allow app to get usage stats, its not android M default way to deal with permissions, we need to check result in onActivityResult

In this sample if user did not grant permission we will ask again while permission is not granted. In real application, its better to tell user why do we need this permission and turn off functionality if permission was not granted.

After the permission was eventually granted, we can query information about last active packages

There are a lot of methods in UsageStatsManager, that can be useful for your app. Checkout official documentation

Full source code for this article is available on GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *