9

我正在使用 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_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_BEST。

我想知道是否有可能获取此持续时间(即不到一天)的数据,因为 INTERVAL_BEST 尚未正确记录以包含此信息。任何帮助将不胜感激,因为我被困在这个问题上。

4

2 回答 2

4

正如UsageStatsManager文档所说:

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

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

于 2015-09-30T18:57:33.370 回答
3

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

您可以使用queryEvents(long startTime, long endTime)from UsageStatsManager 来实现所需的结果。该方法可能看起来像这样(受这篇文章的启发):

@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();
于 2020-05-04T15:13:50.593 回答