Android UsageStatsManager producing wrong output?

99封情书 提交于 2019-12-01 05:22:19

问题


I am using this link to produce app usage states. My understanding with chosen interval is that for YEARLY interval, it aggregates data for each YEAR for each package between beginTime and endTime duration. Similarly, it should work for WEEKLY and DAILY intervals.

With WEEKLY, my code and output is given below;

Code:

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);
}

output:

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 = 

However, if I use INTERVAL.DAILY for the above code, I only get the first three lines. My interval is big enough that it should contain all the data for all the packages between this interval.

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

Am I doing something wrong or my understanding about aggregate intervals is not correct? Any help would be appreciated. Thanks.

Edit1

After looking into the statsfile android stores at /data/system/usagestats folder, I found two files for daily stats and one for weekly stats as given below.

daily file1

<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>

daily file2

<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>

Weekly

<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>

This whole data seems to be messed up. For same browser app, daily stores different results and weekly stores different.


回答1:


According to google release documentation.

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

The system collects the usage data on a per-app basis, aggregating the data over daily, weekly, monthly, and yearly intervals. The maximum duration that the system keeps this data is as follows:

Daily data: 7 days

Weekly data: 4 weeks

Monthly data: 6 months

Yearly data: 2 years

If you want to get stats for older than 7 seven days changes time interval from INTERVAL_DAILY to INTERVAL_WEEKLY or INTERVAL_YEARLY. here is more detail about the same.

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




回答2:


It is probably messed up. You can't trust the result of queryUsageStats.

In my case, invoking

queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0, today) 

shows results for the last seven days and invoking

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

only shows results for today. few-days-ago and today are the time in milliseconds for the indicated day. Checking the results for today shows that usage of a certain app is missing while other apps used later today are present.



来源:https://stackoverflow.com/questions/30386789/android-usagestatsmanager-producing-wrong-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!