0

如果这是一个重复的帖子,我很抱歉,但我已经查看了几个问题,但似乎没有一个对我有帮助。所以,我试图调用 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");
    }
4

1 回答 1

0

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

于 2019-06-27T14:29:43.717 回答