使用UsageEvents,UsageEvents.Event和MOVE_TO_FOREGROUND计算一天中每个应用的使用次数 [英] Count how many times every app is used in one day using UsageEvents, UsageEvents.Event and MOVE_TO_FOREGROUND

查看:815
本文介绍了使用UsageEvents,UsageEvents.Event和MOVE_TO_FOREGROUND计算一天中每个应用的使用次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用UsageStatsManager和UsageStats,我可以获取智能手机上每个应用程序的每日使用情况(小时,分钟和秒).现在,我想知道是否有一种方法可以获取智能手机上每个应用程序的每日使用频率(以次数为单位,即1次,2次等).

With UsageStatsManager and UsageStats I'm able to obtain the daily usage (in hours, minutes and seconds) of every app on my smartphone. Now I want to know if there is a way to obtain the daily frequency of use (in numbers of times, i.e. 1 time, 2 times, etc) of every app on my smartphone.

阅读文档后,我推断可以使用UsageEvents,UsageEvents.Event和MOVE_TO_FOREGROUND来实现.所以我写了下面的代码:

Reading the documentation I deduced that this could be achieved using UsageEvents, UsageEvents.Event and MOVE_TO_FOREGROUND. So I wrote the following code:

// Get the app statistics since one day ago from the current time.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, -1);

/* Query for events in the given time range. Events are only kept by the system for a few days.
 * NOTE: The last few minutes of the event log will be truncated to prevent abuse by applications. */
UsageEvents queryEvents = mUsageStatsManager.queryEvents(
            cal.getTimeInMillis(),     //begin-time
            System.currentTimeMillis()); //end-time

ArrayList<String> packagesNames = new ArrayList<>();

// I get a list with package names having an event of "MOVE_TO_FOREGROUND"
while (queryEvents.hasNextEvent()) {
        UsageEvents.Event eventAux = new UsageEvents.Event();
        queryEvents.getNextEvent(eventAux);
        if (eventAux.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            packagesNames.add(eventAux.getPackageName());
            System.out.println(eventAux.getPackageName()+" "+eventAux.MOVE_TO_FOREGROUND );
        }
}

// I count the occurrences of each packet name.
Set<String> unique = new HashSet<String>(packagesName);
for (String key : unique) {
        System.out.println(key + ": " + Collections.frequency(packagesName, key));
}

但是经过几次测试,我注意到,例如,如果我一次打开Whatsapp,其每日使用次数将增加2个单位(而不是增加1个单位),而对于其他应用程序(例如Facebook或默认浏览器),该计数为好的.

But after several tests I noticed that for example, if I open Whatsapp once, its daily use count is increased by 2 units (and not by 1 unit), while for other apps like Facebook or the default browser, the count is ok.

代码中是否有任何错误?为什么它不适用于所有已安装的应用程序?还有另一种方法来获得相同的结果吗?

Is there any bug in the code? Why does it not work for all installed applications? Is there another way to get the same result?

推荐答案

你好,我在尝试这段代码,在whatsapp上也遇到了同样的问题,但是我尝试删除此代码

hello i was trying this code, i also got the same problem on whatsapp, but i try to remove this code

System.out.println(eventAux.getPackageName()+" "+eventAux.MOVE_TO_FOREGROUND );

工作正常,计数正确,谢谢

and it work, the count is correct, thank you

这篇关于使用UsageEvents,UsageEvents.Event和MOVE_TO_FOREGROUND计算一天中每个应用的使用次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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