How to Use Usagestatsmanager

How to use UsageStatsManager?

I think the documentation was just short hand for the Calendar stuff. I don't think it actually works with just 2014; however I can be wrong.

In order to access the actually list of UsageStats, you would need to create a Calendar object with the correct month,day, and year. Exactly how MRK said in the other answer. I copied and corrected the errors in MRK's code so anyone who sees it in the future can see it.

Calendar beginCal = Calendar.getInstance();
beginCal.set(Calendar.DATE, 1);
beginCal.set(Calendar.MONTH, 0);
beginCal.set(Calendar.YEAR, 2012);

Calendar endCal = Calendar.getInstance();
endCal.set(Calendar.DATE, 1);
endCal.set(Calendar.MONTH, 0);
endCal.set(Calendar.YEAR, 2014);

final List<UsageStats> queryUsageStats=usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, beginCal.getTimeInMillis(), endCal.getTimeInMillis());

-Credit MRK; corrected by me (he accidentally just put cal instead of beginCal and endCal)

The code for the usage access settings is below. :)

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);

How to use UsageStatsManager on Android Wear

Usage Permission intent isn't possible on Wear.

Same goes for Overlay (Settings.canDrawOverlays()) though at least you can use lower target to overcome it.

Also with latest Android N emulator this isn't resolved.

I want to use UsageStatsManager in my service

UsageStatsManager doesn't update the unless the app goes in background.
You can archive this by adding the difference of current time and timestamp of current running app going foreground.

Refer to this answer for details : How to count app usage time while app is on foreground?

How do I track app usage time in android?

I've done a fair bit of work in this area. I'd like to direct you to my smartCBT GitHub that reviews in a given period of time what a person has been using their apps for (https://github.com/kris-geyer/smartCBT; in particular, check out https://github.com/kris-geyer/smartCBT/blob/master/app/src/main/java/kg/own/smartcbt/ViewModel/RetrieveUsageRecords.java).

First off, I wouldn't trust the usage stats manager. It's been very unreliable in the past for me. I think what you want to assess usage is usage events (https://developer.android.com/reference/android/app/usage/UsageEvents.Event). These give you a much more fine-grained account of android app usage which seem much more accurate. You will need to get special permission for this. I've actually, empirically tested the accuracy before and it is very high.

For the overall structure of what your app needs I think you want a foreground service that runs in the background routinely logging what the person is using their phone for. This database is only updated when apps are changed, so handling this might be a little tricky but you'll manage.

When the app has identified that the allotted amount of usage time for a particular app is up, then it can direct the user back to your app by launching a new activity from the foreground service. I'm not sure if this is still possible, the android permissions seem to change so quickly. What would be really cool is if you can get the app to play videos of the user telling themselves that they've used their phone too much and be productive.

Best of luck. :)



Related Topics



Leave a reply



Submit