Search code examples
androidusagestatsmanager

How can I get my app screen time on a hourly basis?


Android's UsageStatsManager returning data for minimum one day, but I want to get accurate data from 12 am to the current time. I tried several types but nothing works properly. The "Action Dash" and "Digital Wellbeing" is getting data accurately.


Solution

  • Use queryEvents method of UsageStatsManager class.

    I've recently built an app for tracking daily app usage. Firstly, I tried using queryUsageStats but it didn't work properly. I thought it was supposed to return usage time from 12 am to current time when INTERVAL_DAILY is passed to it, but instead it was returning something else. Maybe there is a bug or there is something wrong on my side related to time zone or something.

    Anyway, then I tried queryEvents and it worked fine.

    Here's my implementation (See both the question and the accepted answer).