Android:UsageStatsManager无法返回正确的每日结果 [英] Android: UsageStatsManager not returning correct daily results

查看:756
本文介绍了Android:UsageStatsManager无法返回正确的每日结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从UsageStatsManager查询UsageStats,目的是返回每天使用的所有应用程序包以及使用了多长时间.

I'm attempting to query UsageStats from UsageStatsManager, with the aim of returning all app packages that were used daily and for how long.

代码:

public static List<UsageStats> getUsageStatsList(Context context){
    UsageStatsManager usm = getUsageStatsManager(context);
    Calendar calendar = Calendar.getInstance();
    long endTime = calendar.getTimeInMillis();
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = calendar.getTimeInMillis();

    List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime, endTime);
    return usageStatsList;
}

我有一个警报,该警报每天在午夜之前触发,并查询查询的用法状态,然后存储返回的数据.最初,一切似乎都工作正常,并且我正在获取软件包结果及其活动时间,但是我添加了一个每小时检查一次结果的函数,这是我一个奇怪的发现的地方.

I have an alarm that fires daily just before midnight and query's usagestats and then stores the returned data. At first everything seemed to be working fine and I was getting package results and their active time, however I added a function that would check the results hourly and here is where I made a strange discovery.

UsageStatsManager的结果似乎是在不同的时间重置的,而不是在午夜,这是考虑到我将INTERVAL_DAILY用作搜索参数时的期望值.

The results from UsageStatsManagerseemed to be resetting at different times, instead of at midnight, which is what I would have expected considering I was using INTERVAL_DAILY as a search parameter.

从我保存的数据包中,时间"结果似乎在(粗略时间)重置:

From the data I saved the package 'time' results seem to be resetting at (Rough timings):

  • 凌晨3点
  • 中午
  • 下午3点
  • 午夜

我意识到重新设置包装时间之间存在关联,但这是否意味着发生?

I realize that there is a correlation between when the package timings reset but is this meant to happen?

我已经看过以下线程,在这里我可以从中获得很多信息: 如何使用UsageStatsManager?

I've already seen the following thread and it's where I got a lot of my information from: How to use UsageStatsManager?

因此: Android UsageStatsManager产生错误的输出? 在评论中提到,不能信任从queryUsageStats返回的数据,并且将返回随机结果.

Consequently: Android UsageStatsManager producing wrong output? In the comments mentions that the data returned from queryUsageStats can't be trusted and random results are being returned.

我错过了一些简单的东西还是UsageStatsManager无法正常工作吗?

Am I missing something simple or is UsageStatsManager not functioning correctly?

推荐答案

我也注意到API 21中的这种行为,在API 21中,UsageStats数据未保持足够长的时间,它在API 22中运行良好,如果您签入android /data/system/usagestats,您会在API 21中找到有限的条目,因此在API 21中使用它并不可靠.

I too noticed this behaviour in API 21,UsageStats data is not maintained for sufficiently long in API 21. it works fine from API 22, If you check in android /data/system/usagestats, you will find limited entries in API 21, so its not reliable using it in API 21.

对于API 21+, 根据UsageStatsManager API查询INTERVAL_DAILY时,您将获得整天usagestats的信息. 如果要在一天中的几个小时内进行查询,则应使用queryEvents并按照自己的逻辑对其进行迭代.

For API 21+, You will get the whole day usagestats while querying INTERVAL_DAILY according to UsageStatsManager API. If you want to query within hours of day you should use queryEvents and iterating them by your own logic.

我以以下方式尝试过...

I tried in the following way...

这是用于捕获每个应用程序数据的模式类:

This is the modal class for capturing data for every app:

private class AppUsageInfo {
        Drawable appIcon;
        String appName, packageName;
        long timeInForeground;
        int launchCount;

        AppUsageInfo(String pName) {
            this.packageName=pName;
        }
}

List<AppUsageInfo> smallInfoList; //global var

这是简单易行的方法,顺其自然:

here is the method, its easy, go with the flow:

void getUsageStatistics() {

UsageEvents.Event currentEvent;
List<UsageEvents.Event> allEvents = new ArrayList<>();
HashMap<String, AppUsageInfo> map = new HashMap <String, AppUsageInfo> ();

long currTime = System.currentTimeMillis();
long startTime currTime - 1000*3600*3; //querying past three hours

UsageStatsManager mUsageStatsManager =  (UsageStatsManager)
                    mContext.getSystemService(Context.USAGE_STATS_SERVICE);

        assert mUsageStatsManager != null;
UsageEvents usageEvents = mUsageStatsManager.queryEvents(usageQueryTodayBeginTime, currTime);

//capturing all events in a array to compare with next element

         while (usageEvents.hasNextEvent()) {
            currentEvent = new UsageEvents.Event();
            usageEvents.getNextEvent(currentEvent);
            if (currentEvent.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND ||
                    currentEvent.getEventType() == UsageEvents.Event.MOVE_TO_BACKGROUND) {
                allEvents.add(currentEvent);
                String key = currentEvent.getPackageName();
// taking it into a collection to access by package name
                if (map.get(key)==null)
                    map.put(key,new AppUsageInfo(key));
            }
        }

//iterating through the arraylist 
         for (int i=0;i<allEvents.size()-1;i++){
            UsageEvents.Event E0=allEvents.get(i);
            UsageEvents.Event E1=allEvents.get(i+1);

//for launchCount of apps in time range
             if (!E0.getPackageName().equals(E1.getPackageName()) && E1.getEventType()==1){
// if true, E1 (launch event of an app) app launched
                 map.get(E1.getPackageName()).launchCount++;
             }

//for UsageTime of apps in time range
            if (E0.getEventType()==1 && E1.getEventType()==2
                    && E0.getClassName().equals(E1.getClassName())){
                long diff = E1.getTimeStamp()-E0.getTimeStamp();
                phoneUsageToday+=diff; //gloabl Long var for total usagetime in the timerange
                map.get(E0.getPackageName()).timeInForeground+= diff;
            }
        }
//transferred final data into modal class object
        smallInfoList = new ArrayList<>(map.values());

}

这篇关于Android:UsageStatsManager无法返回正确的每日结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆