UsageStatsManager wrong time range

228 Views Asked by At

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

0

There are 0 best solutions below