UsageStatsManager的每日间隔错误

时间:2018-06-11 12:29:16

标签: android usage-statistics

我尝试按UTC从21:00到21:00获取昨天的应用使用统计数据,我的时区为UTC + 3。 我的代码:

    private String createReport() {
    UsageStatsManager usageStatsManager = (UsageStatsManager) getApplication().getSystemService(Context.USAGE_STATS_SERVICE);
    if (usageStatsManager != null) {
        List<UsageStats> usageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, getDateMillis(-1).first, getDateMillis(0).first);
        Log.i("Time", String.valueOf(getDateMillis(-1).first) + " " + String.valueOf(getDateMillis(0).first));
        JSONArray array = new JSONArray();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (UsageStats temp: usageStats) {
            if (temp.getTotalTimeInForeground() > 0) {
                JSONObject post = new JSONObject();
                try {
                    post.put("Name", temp.getPackageName());
                    post.put("Duration", temp.getTotalTimeInForeground());
                    post.put("Date", dateFormat.format(temp.getLastTimeUsed()));
                    post.put("StartDate", dateFormat.format(temp.getFirstTimeStamp()));
                    post.put("LastDate", dateFormat.format(temp.getLastTimeStamp()));
                    post.put("MilisStartRange", String.valueOf(temp.getFirstTimeStamp()));
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                array.put(post);
            }
        }
        Log.i("Usage:", array.toString());
        return array.toString();
    }
    return "";
}

private Pair<Long,String> getDateMillis(long Day) {
    long result = System.currentTimeMillis();
    String date = " ";
    try {
        DateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd");
        date = dateFormat.format(new Date());
        Date now = dateFormat.parse(dateFormat.format(new Date()));
        result = now.getTime() + Day * 86400000;
        date = dateFormat.format(new Date(result));
    } catch (Exception e) {}
    return new Pair<>(result, date);
}

usageStatsManager.queryUsageStats的输入参数是:

I/Time: 1528578000000 1528664400000

来自

Sat Jun 09 2018 21:00:00 UTC and Sun Jun 10 2018 00:00:00 Local

Sun Jun 10 2018 21:00:00 UTC and Mon Jun 11 2018 00:00:00 Local.

但不是那段时间我得到这样的输出:

[{"Name":"com.google.android.youtube","Duration":10250,"Date":"2018-06-10 00:25:31","StartDate":"2018-06-09 14:30:01","LastDate":"2018-06-10 14:30:01","MilisStartRange":"1528543801139"},
...
{"Name":"com.lge.qmemoplus","Duration":10405,"Date":"2018-06-11 10:45:41","StartDate":"2018-06-10 14:39:18","LastDate":"2018-06-11 14:33:06","MilisStartRange":"1528630758448"}]

系统给我2个间隔:

from "2018-06-09 14:30:01" to "2018-06-10 14:30:01" 
and
from "2018-06-10 14:39:18" to "2018-06-11 14:33:06"

由我当地的UTC + 3

我的设备是带有Android 7.1.2(API 25)的LG V30 +,也是

compileSdkVersion 27
defaultConfig {
    applicationId "com.example.yas.apptracker"
    minSdkVersion 22
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

请帮助我,我无法理解错误是什么,以及是否有可能解决这个问题

此外,我今天在模拟器(也是25)中得到了类似的结果:

{"Name":"com.android.settings","Duration":885073,"Date":"2018-06-11 15:14:38","StartDate":"2018-06-11 10:42:20","LastDate":"2018-06-11 15:30:51","MilisStartRange":"1528702940536"},{"Name":"com.android.systemui","Duration":10705,"Date":"2018-06-11 15:14:39","StartDate":"2018-06-11 10:42:20","LastDate":"2018-06-11 15:30:51","MilisStartRange":"1528702940536"}

0 个答案:

没有答案