I'm attempting to query UsageStats from UsageStatsManager, with the aim of returning all app packages that were used daily and for how long.
The Code:
public static List<UsageStats> getUsageStatsList(Context context){
UsageStatsManager usm = getUsageStatsManager(context);
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.DAY_OF_YEAR, -1);
long startTime = calendar.getTimeInMillis();
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime, endTime);
return usageStatsList;
}
I have an alarm that fires daily just before midnight and query's usagestats and then stores the returned data. At first everything seemed to be working fine and I was getting package results and their active time, however I added a function that would check the results hourly and here is where I made a strange discovery.
The results from UsageStatsManagerseemed to be resetting at different times, instead of at midnight, which is what I would have expected considering I was using INTERVAL_DAILY as a search parameter.
From the data I saved the package 'time' results seem to be resetting at (Rough timings):
- 3am
- Midday
- 3pm
- Midnight
I realize that there is a correlation between when the package timings reset but is this meant to happen?
I've already seen the following thread and it's where I got a lot of my information from: How to use UsageStatsManager?
Consequently:
Android UsageStatsManager producing wrong output?
In the comments mentions that the data returned from queryUsageStats can't be trusted and random results are being returned.
Am I missing something simple or is UsageStatsManager not functioning correctly?