queryUsageStats间隔持续时间

时间:2015-06-16 07:46:47

标签: android usagestatsmanager

我正在使用UsageStatsManager API来获取特定时间间隔的使用情况统计信息。如果我使用预定义的间隔,即INTERVAL_DAILY,INTERVAL_WEEKLY,INTERVAL_MONTHLY,INTERVAL_YEARLY,则一切正常。但是,如果我想查看过去2或3个小时的数据,我将获得今天的数据。我尝试过使用Calendars和System.currentTimeMillis(),但是没有给我过滤结果。

日历方法:

Calendar startCalendar = Calendar.getInstance();
startCalendar.add(Calendar.HOUR_OF_DAY, -2);

Calendar endCalendar = Calendar.getInstance();

并将此传递给queryUsageStats方法,如下所示:

usageList = usm.queryUsageStats(interval, startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis());

其中interval是INTERVAL_BEST。

System.currentTimeMillis()方法:

long startTime = System.currentTimeMillis() - 7200*1000 // 7200 seconds i.e. 2 hrs

long endTime = System.currentTimeMillis();

将此传递给queryUsageStats,如上所述:

usageList = usm.queryUsageStats(interval, startTime, endTime);

其中interval再次为INTERVAL_BEST。

我想知道是否有可能在此期间(即不到一天)获取数据,因为尚未正确记录INTERVAL_BEST以包含此信息。任何帮助都会受到赞赏,因为我坚持这个问题。

2 个答案:

答案 0 :(得分:2)

正如UsageStatsManager doc所说:

  

在时间间隔中间的数据请求将包括该间隔。

似乎使用数据存储在存储桶中,最小存储桶是一天,因此您无法在不到一天的时间内查询使用情况统计信息。即使您查询特定日期的一小时间隔,也会返回整天的使用情况统计信息。

答案 1 :(得分:2)

聚会晚了一点,但是我认为这可能对某些人有用。

您可以使用UsageStatsManager中的queryEvents(long startTime, long endTime)获得所需的结果。该方法可能如下所示(受this post启发):

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public HashMap<String, AppUsageInfo> queryUsageStatistics(Context context, long startTime, long endTime) {
    UsageEvents.Event currentEvent;
    List<UsageEvents.Event> allEvents = new ArrayList<>();
    HashMap<String, AppUsageInfo> map = new HashMap<>();
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
    assert mUsageStatsManager != null;
    // Here we query the events from startTime till endTime.
    UsageEvents usageEvents = mUsageStatsManager.queryEvents(startTime, endTime);

    // go over all events.
    while (usageEvents.hasNextEvent()) {
        currentEvent = new UsageEvents.Event();
        usageEvents.getNextEvent(currentEvent);
        String packageName = currentEvent.getPackageName();
        if (currentEvent.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED || currentEvent.getEventType() == UsageEvents.Event.ACTIVITY_PAUSED ||
                    currentEvent.getEventType() == UsageEvents.Event.ACTIVITY_STOPPED) {
            allEvents.add(currentEvent); // an extra event is found, add to all events list.
            // taking it into a collection to access by package name
            if (!map.containsKey(packageName)) {
                map.put(packageName, new AppUsageInfo());
            }
        }
    }

    // iterate through all events.
    for (int i = 0; i < allEvents.size() - 1; i++) {
        UsageEvents.Event event0 = allEvents.get(i);
        UsageEvents.Event event1 = allEvents.get(i + 1);

        //for launchCount of apps in time range
        if (!event0.getPackageName().equals(event1.getPackageName()) && event1.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED) {
            // if true, E1 (launch event of an app) app launched
            Objects.requireNonNull(map.get(event1.getPackageName())).launchCount++;
        }

        //for UsageTime of apps in time range
        if (event0.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED &&
                (event1.getEventType() == UsageEvents.Event.ACTIVITY_PAUSED || event1.getEventType() == UsageEvents.Event.ACTIVITY_STOPPED)
                && event0.getPackageName().equals(event1.getPackageName())) {
            long diff = event1.getTimeStamp() - event0.getTimeStamp();
            Objects.requireNonNull(map.get(event0.getPackageName())).timeInForeground += diff;
        }
    }
    // and return the map.
    return map;
}

AppUsageInfo类为:

public class AppUsageInfo {

    public long timeInForeground;
    public int launchCount;

    AppUsageInfo() {
        this.timeInForeground = 0;
        this.launchCount = 0;
    }
}

要获取最近两个小时的使用情况统计信息,只需致电

Calendar startCalendar = Calendar.getInstance();
startCalendar.add(Calendar.HOUR_OF_DAY, -2);
Calendar endCalendar = Calendar.getInstance();

HashMap<String, AppUsageInfo> result = queryUsageStatistics(context, startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis();