queryEvents调用不返回任何内容

时间:2019-06-17 22:17:38

标签: android usagestatsmanager usageevents

很抱歉,如果这是重复的帖子,但是我看了几个问题,但似乎没有一个对我有帮助。所以,我试图调用queryEvents()但什么也没得到。我在这里附加我的代码,如果有人能指出我所缺少的内容,我将不胜感激。它永远不会进入while循环

    final SimpleDateFormat dateFormat = new SimpleDateFormat("M-d-yyyy HH:mm:ss");
    UsageStatsManager usm = (UsageStatsManager) mContext.getSystemService(Context.USAGE_STATS_SERVICE);
    long startTime, endTime;

    endTime = System.currentTimeMillis();
    startTime = endTime - (1000*3600*3);

    Log.d(TAG, "Range start:" + dateFormat.format(startTime) );
    Log.d(TAG, "Range end:" + dateFormat.format(endTime));

    UsageEvents uEvents = usm.queryEvents(startTime, endTime);

    while (uEvents.hasNextEvent())
    {
        UsageEvents.Event e = new UsageEvents.Event();
        uEvents.getNextEvent(e);

        Log.d(TAG, "inside while...");

        if (e != null)
        {
            Log.d(TAG, "Event: " + e.getPackageName() + "\t" +  e.getTimeStamp());
        }

        else
            Log.d(TAG, "e=null");
    }

1 个答案:

答案 0 :(得分:0)

您的代码对我有用。您确定您拥有android.permission.PACKAGE_USAGE_STATS的权限吗?