wrkm.app
Search
Mobile Development

Why UsageStatsManager returned no foreground app on a real Android phone

Why UsageStatsManager returned no foreground app on a real Android phone

Editor
Nov 26, 2026 · 3 min read
Why UsageStatsManager returned no foreground app on a real Android phone

If UsageStatsManager.queryEvents(now - 10s, now) returns nothing on a real phone, check dumpsys usagestats for a daily bucket dated in the future. Android files new events into that bucket after the clock was once ahead, and a query that ends at now never searches it. Query a few days past now, then ignore events stamped after now.I hit this while building Focus Sprint, my focus timer for ADHD and neurodivergent brains, in Flutter for Android and iPhone. One of its features is a soft app shield: you pick the apps that pull you away, and during a sprint opening one of them shows a calm “not now” screen instead.How an Android app blocker knows which app is openOn iPhone that’s Apple’s Screen Time API. Android has no such API, so the shield is a foreground service: every 700 ms it asks UsageStatsManager which app came to the foreground in the last ten seconds and, if it's on the list, draws an overlay window on top of it.Symptom: works on the emulator, nothing on a real phoneThe first build from Google Play went onto a budget Motorola. Shield on, sprint started, Telegram opened. Nothing. No pause screen, no error.The service itself was fine: dumpsys activity services showed it running in the foreground, both permissions granted, the blocked list correct. So I added one log line with the input to the decision rather than the outcome:fg=null block=false list=[org.telegram.messenger, ...] overlayPerm=trueThe service simply never learned which app was open.Why queryEvents returns nothing: a stats bucket from the futuredumpsys usagestats explained it. The "current" daily stats bucket on the phone was dated 23-24 October. It was 22 October. At some point the phone's clock had run ahead, and Android kept filing every new event into that future-dated bucket even after the clock was corrected.My query asked for events between ten seconds ago and now. Android only looks in buckets that start before the end of the query, so the bucket holding all of today’s events was never searched. The answer was always empty.What you see What it meansEmpty events, service running The query never reached today's bucketCurrent bucket dated tomorrow The device clock was ahead at some pointApp "last used" in the future Same cause: it looks like the foreground app foreverThe fix: query past now, then drop the futureQuery a week past now, so a future-dated bucket is still searched. Event timestamps themselves are real, so nothing extra matches.Ignore anything stamped after now. Fixing the window surfaced a system app whose “last used” time was two days ahead: it would have counted as the foreground app forever.Fall back to aggregated usage stats when there are no foreground events at all, and take the most recently used app.usm.queryEvents(now - 10_000, now + CLOCK_SLACK_MS) // newest MOVE_TO_FOREGROUND with timeStamp <= now + 5 s?: usm.queryUsageStats(INTERVAL_DAILY, now - 1h, now + CLOCK_SLACK_MS) .filter { it.lastTimeUsed in 1..(now + 5_000) } .maxByOrNull { it.lastTimeUsed }Checklist for usage-access features on AndroidTest on the cheapest phone you can find, not just the emulator and your own flagship.When something silently does nothing, log the inputs to the decision. “It didn’t block” told me nothing; fg=null pointed straight at the cause.Never assume the device clock has always been right.Focus Sprint: ADHD Focus Timer helps you start what you keep putting off: 5-minute sprints, a kind focus buddy and a soft app blocker. It’s free to download on Google Play.I’m Andrii Zhukovskyi, an independent developer. Under the name Dre Dev I build Flutter apps for Android and iPhone: all my apps, plus free calculators for HVAC, electrical and other trades. Some of the apps still need testers: become a tester, it takes two minutes.Originally published at https://zhukovskyi.space on October 24, 2026.Why UsageStatsManager returned no foreground app on a real Android phone was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.

Kaynak: Andrii Zhukovskyi

#Android#Kotlin
Share

From other categories