1

I want to get the usagetimes of my apps by using the UsageStatsManager.queryUsageStats method.

Calendar currentDay=Calendar.getInstance();
        currentDay.setTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 1);
        long start = calendar.getTimeInMillis();
        System.out.println("Startdate "+df.format(new Date(start)));
        long end = System.currentTimeMillis();

        final List<UsageStats> usageStatsList=usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,start,end);

Startdate is correct :I/System.out: Startdate 30.10.19 00:01

But when I iterate through the entries:

        for(UsageStats usageStats :usageStatsList){
            if(isCheckApplication(usageStats.getPackageName()) && usageStats.getTotalTimeInForeground()>0) {
                foreGround = foreGround + usageStats.getTotalTimeInForeground();
                System.out.println(usageStats.getPackageName()+" "+df.format(usageStats.getFirstTimeStamp())+" "+df.format(usageStats.getLastTimeUsed())+" "+usageStats.getTotalTimeInForeground()/1000/60);
                }
        }

Also entries for the last day are counted:

org.wikipedia 29.10.19 18:42 29.10.19 23:46 1

com.android.chrome 29.10.19 18:42 30.10.19 08:34 41 ...

And also foreground time is counted for this interval. How can I restrict the interval properly

Nico Hennrich
  • 214
  • 2
  • 10
  • Might be the same problem as described here: https://stackoverflow.com/a/53815618/10375027 – BBB Nov 14 '19 at 11:05
  • 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:56

0 Answers0