6

我正在使用此链接生成应用程序使用状态。我对所选间隔的理解是,对于间隔,它为每个包之间的每个包和持续时间YEARLY聚合数据。同样,它应该适用于和间隔。YEARbeginTimeendTimeWEEKLYDAILY

使用WEEKLY,我的代码和输出如下所示;

代码:

Calendar beginCal = Calendar.getInstance();
beginCal.set(Calendar.DATE, 1);
beginCal.set(Calendar.MONTH, 0);
beginCal.set(Calendar.YEAR, 2012);

Calendar endCall = Calendar.getInstance();
endCall.set(Calendar.DATE, 1);
endCall.set(Calendar.MONTH, 0);
endCall.set(Calendar.YEAR, 2016);

final List<UsageStats> queryUsagesStats= mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_WEEKLY, beginCal.getTimeInMillis(), endCall.getTimeInMillis());    

for(UsageStats us: queryUsagesStats)
{
    long beginTime = us.getFirstTimeStamp();
    String beginDate = df.format(new Date(beginTime));


    long endTime = us.getLastTimeStamp();
    String endDate = df.format(new Date(endTime));
    String lastTime = df.format(new Date(us.getLastTimeUsed()));
    long totalTimeInForeground = us.getTotalTimeInForeground()/1000L;
    System.out.println("Pkg = " + us.getPackageName() + ", beginTime " + beginDate + ", endTime= " + endDate + ", lastTime = " + lastTime + ", totalTime = " + totalTimeInForeground);
}

输出:

Pkg = com.example.newappusagestatistics, beginTime 05/20/2015 17:00:04, endTime= 05/21/2015 17:55:42, lastTime = 05/21/2015 17:55:42, totalTime = 11186
Pkg = com.android.launcher, beginTime 05/20/2015 17:00:04, endTime= 05/21/2015 17:55:42, lastTime = 05/21/2015 17:55:42, totalTime = 1091
Pkg = com.android.browser, beginTime 05/20/2015 17:00:04, endTime= 05/21/2015 17:15:27, lastTime = 05/21/2015 17:15:27, totalTime = 68
Pkg = com.android.sdksetup, beginTime 05/20/2015 17:00:04, endTime= 01/01/1970 15:59:55, lastTime = 05/21/2015 14:41:08, totalTime = 1194
Pkg = com.android.camera, beginTime 05/20/2015 17:00:04, endTime= 01/01/1970 15:59:55, lastTime = 05/21/2015 14:55:32, totalTime = 40
Pkg = com.android.settings, beginTime 05/20/2015 17:00:04, endTime= 01/01/1970 15:59:55, lastTime = 05/21/2015 16:15:19, totalTime = 97
Pkg = com.android.systemui, beginTime 05/20/2015 17:00:04, endTime= 01/01/1970 15:59:55, lastTime = 05/21/2015 14:47:45, totalTime = 

但是,如果我使用INTERVAL.DAILY上面的代码,我只会得到前三行。我的间隔足够大,它应该包含此间隔之间所有包的所有数据。

Pkg = com.example.newappusagestatistics, beginTime 05/21/2015 17:00:00, endTime= 05/21/2015 17:59:18, lastTime = 05/21/2015 17:59:18, totalTime = 3484
Pkg = com.android.launcher, beginTime 05/21/2015 17:00:00, endTime= 05/21/2015 17:59:18, lastTime = 05/21/2015 17:59:18, totalTime = 27
Pkg = com.android.browser, beginTime 05/21/2015 17:00:00, endTime= 05/21/2015 17:15:27, lastTime = 05/21/2015 17:15:27, totalTime = 43

我是不是做错了什么,或者我对聚合间隔的理解不正确?任何帮助,将不胜感激。谢谢。

编辑1

在查看 statsfile android 存储在 /data/system/usagestats 文件夹后,我发现了两个用于每日统计的文件和一个用于每周统计的文件,如下所示。

每日档案1

<usagestats version="1" endTime="59366071">
<packages>
<package lastTimeActive="59366071" package="com.example.newappusagestatistics" timeActive="4064908" lastEvent="1"/>
<package lastTimeActive="59366020" package="com.android.launcher" timeActive="813728" lastEvent="2"/>
<package lastTimeActive="927475" package="com.android.browser" timeActive="43838" lastEvent="2"/>
</packages>

每日档案2

<usagestats version="1" endTime="86395657">
    <packages>
        <package lastTimeActive="86395657" package="com.example.newappusagestatistics" timeActive="7916365" lastEvent="3" />
        <package lastTimeActive="85743386" package="com.android.launcher" timeActive="1064651" lastEvent="2" />
        <package lastTimeActive="78961499" package="com.android.browser" timeActive="24715" lastEvent="2" />
        <package lastTimeActive="78063875" package="com.android.sdksetup" timeActive="1194161" lastEvent="3" />
        <package lastTimeActive="78928555" package="com.android.camera" timeActive="40775" lastEvent="2" />
        <package lastTimeActive="83715390" package="com.android.settings" timeActive="97012" lastEvent="2" />
        <package lastTimeActive="78461328" package="com.android.systemui" timeActive="4162" lastEvent="2" />
    </packages>

每周

<usagestats version="1" endTime="145761729">
    <packages>
        <package lastTimeActive="145761729" package="com.example.newappusagestatistics" timeActive="11981273" lastEvent="1" />
        <package lastTimeActive="145761678" package="com.android.launcher" timeActive="1878379" lastEvent="2" />
        <package lastTimeActive="87323133" package="com.android.browser" timeActive="68553" lastEvent="2" />
        <package lastTimeActive="78063875" package="com.android.sdksetup" timeActive="1194161" lastEvent="3" />
        <package lastTimeActive="78928555" package="com.android.camera" timeActive="40775" lastEvent="2" />
        <package lastTimeActive="83715390" package="com.android.settings" timeActive="97012" lastEvent="2" />
        <package lastTimeActive="78461328" package="com.android.systemui" timeActive="4162" lastEvent="2" />
    </packages>

整个数据似乎一团糟。对于同一个浏览器应用,每天存储不同的结果,每周存储不同。

4

2 回答 2

9

根据谷歌发布文档。

https://developer.android.com/about/versions/android-5.0.html#System

系统以每个应用为基础收集使用数据,并在每天、每周、每月和每年的时间间隔内汇总数据。系统保留此数据的最长时间如下:

每日数据:7天

每周数据:4周

月度数据:6个月

年数据:2年

如果您想获取超过 7 天的统计信息,请将时间间隔从 INTERVAL_DAILY 更改为 INTERVAL_WEEKLY 或 INTERVAL_YEARLY。这里有更多关于相同的细节。

https://developer.android.com/reference/android/app/usage/UsageStatsManager.html

于 2016-05-04T07:11:17.697 回答
3

大概是搞砸了。您不能相信 queryUsageStats 的结果。

就我而言,调用

queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0, today) 

显示过去 7 天的结果并调用

queryUsageStats(UsageStatsManager.INTERVAL_DAILY, few-days-ago, today) 

只显示今天的结果。几天前和今天是指定日期的时间(以毫秒为单位)。检查今天的结果表明,某个应用程序的使用缺失,而今天晚些时候使用的其他应用程序存在。

于 2015-11-01T16:06:07.597 回答