0

I am trying to use the UsageStatsManager class' queryUsageStats method in order to retrieve only the usage statistics for each package on the device for the current day (00:00:00 to 23:59:59). The code in question is as follows:

long startOfToday = TimeUtility.getStartOfToday();
long endOfToday = TimeUtility.getEndOfToday();
List<UsageStats> usageStats = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startOfToday, endOfToday);

Methods for retrieving start and end of day:

    public static long getStartOfToday()
    {
        Calendar cStartOfDay = Calendar.getInstance();
        cStartOfDay.set(Calendar.HOUR_OF_DAY, 0);
        cStartOfDay.set(Calendar.MINUTE, 0);
        cStartOfDay.set(Calendar.SECOND, 0);
        cStartOfDay.set(Calendar.MILLISECOND, 0);
        return cStartOfDay.getTimeInMillis();
    }

    public static long getEndOfToday()
    {
        Calendar cEndOfDay = Calendar.getInstance();
        cEndOfDay.set(Calendar.HOUR_OF_DAY, 23);
        cEndOfDay.set(Calendar.MINUTE, 59);
        cEndOfDay.set(Calendar.SECOND, 59);
        cEndOfDay.set(Calendar.MILLISECOND, 999);
        return cEndOfDay.getTimeInMillis();
    }

Based on my understanding of the documentation, I have passed in the appropriate parameters for what I am trying to achieve.

Bizarrely, however, upon inspection of the UsageStats objects that are returned to the list during debugging, many have an mBeginTimeStamp value (long value representing the beginning of the time range the UsageStats object represents, measured in milliseconds since the epoch) that represents a time from the prior day. For example while testing today on the 13th of December at 11am, an mBeginTimeStamp value from one package in the list pointed to 1pm on the 12th of December.

As far as I am concerned, these values are representations of the time in my local time zone and not in UTC time.

  • That seems the same issue as described here https://stackoverflow.com/questions/53730599/how-to-get-usage-stats-for-current-day-using-usagestatsmanager-in-android-kot/53815618#53815618. As of now I consider this to be a bug... – BBB Dec 18 '19 at 10:47
  • Does this answer your question? [How can I get my app screen time on a hourly basis?](https://stackoverflow.com/questions/61615457/how-can-i-get-my-app-screen-time-on-a-hourly-basis) – Tanjim Ahmed Khan May 15 '20 at 10:55

0 Answers0