1

I'm sorry if this is a repeat post, but I've looked at several questions and none of them seem to help me. So, I'm trying to call queryEvents() but I get nothing. I'm attaching my code here, would appreciate if anyone can point out what I'm missing. It never enters the while loop

    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");
    }
droid_dev
  • 303
  • 4
  • 15

1 Answers1

0

Your code works for me. Did you make sure that you have the permission android.permission.PACKAGE_USAGE_STATS?

BBB
  • 302
  • 2
  • 10
  • Thanks for trying, yes I do have that permission, but no luck. I even removed all sepolicy rules – droid_dev Jun 28 '19 at 20:24
  • Hmm... don't know then, as I said the code works well for me (if my package has above permission). You might want to show us your complete code for your activity and the respective manifest of your package to allow for further help. – BBB Jul 01 '19 at 11:38