Menu

Detecting the foreground app on Android: UsageStatsManager vs AccessibilityService
📰

Detecting the foreground app on Android: UsageStatsManager vs AccessibilityService

DEV Community: kotlin·Adem Ladhari·6 days ago
#Cn2OTyzH
#devtokotlin#devto#dev#event#screen
Long article (869 words) — no AI summary yet.
Reading 0:00
15s threshold

If you're building an app blocker, a screen-time tracker or a parental-control app on Android, the first question is always the same: which app is on screen right now?

Android gives you two realistic answers: UsageStatsManager and AccessibilityService . I use both in Focsy: Doomscrolling Blocker , for different jobs. Here's how each works, what it can and can't see, and when to pick which.

Option 1: UsageStatsManager

UsageStatsManager gives you a log of app usage events: which app moved to the foreground, when, and for how long apps were used.

Permission

It needs PACKAGE_USAGE_STATS , which is a special permission.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.