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. You declare it in the manifest, but the user has to grant it manually in Settings:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" /> 

Check whether it's granted with AppOpsManager, and send the user to Settings.ACTION_USAGE_ACCESS_SETTINGS if it isn't:

fun hasUsageAccess(context: Context): Boolean { val appOps = context.getSystemService(AppOpsManager::class.java) val mode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { appOps.unsafeCheckOpNoThrow( AppOpsManager.OPSTR_GET_USAGE_STATS, Process.myUid(), context.packageName ) } else { @Suppress("DEPRECATION") appOps.checkOpNoThrow( AppOpsManager.OPSTR_GET_USAGE_STATS, Process.myUid(), context.packageName ) } return mode == AppOpsManager.MODE_ALLOWED } 

Finding the foreground app

There's no callback. You query recent events and take the last app that came to the foreground:

fun foregroundPackage(usm: UsageStatsManager): String? { val end = System.currentTimeMillis() val events = usm.queryEvents(end - 10_000, end) val event = UsageEvents.Event() var last: String? = null while (events.hasNextEvent()) { events.getNextEvent(event) if (event.eventType == UsageEvents.Event.ACTIVITY_RESUMED) { last = event.packageName } } return last } 

(ACTIVITY_RESUMED is API 29+. On older versions the same value is MOVE_TO_FOREGROUND.)

The catches

  • You have to poll. No event tells you the user just opened TikTok. You run this every second or so, usually from a foreground service, which costs battery and adds delay.
  • You only see the app (and activity), not the screen. Instagram's Reels and its DMs can live in the same activity, so usage stats can't tell them apart.

Where it shines

Statistics. For a screen-time dashboard, usage stats are exactly right: the data already exists, Android collects it whether or not your app runs, and you don't need to track anything yourself.

One caveat: queryUsageStats and queryAndAggregateUsageStats return data in time buckets, which can include usage from outside the range you asked for. If you need an exact "today" number, add up foreground time from the raw events instead.

Option 2: AccessibilityService

An accessibility service receives events as the screen changes, and it can read the view tree of the app in front.

override fun onAccessibilityEvent(event: AccessibilityEvent) { if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { val pkg = event.packageName?.toString() // the app in front val cls = event.className?.toString() // the window that opened // decide whether to block } } 

Why blockers use it

  • It's instant. The event arrives the moment the window changes. No polling.
  • It can see inside apps. With canRetrieveWindowContent you can inspect the view tree and tell Instagram's Reels screen apart from its DMs. That's the only way to block part of an app.
  • It can act. performGlobalAction(GLOBAL_ACTION_HOME) sends the user to the home screen.

The catches

  • It's sensitive. The service can read screen content, so users are rightly cautious. Google Play requires you to declare how you use the Accessibility API and to show a prominent disclosure before the user turns it on. Use it only for what you declared, and don't send screen content anywhere.
  • Phone makers kill it. Aggressive battery managers on some brands stop accessibility services. Your onboarding needs to deal with that; dontkillmyapp.com is the best reference.
  • Event volume. Content-change events fire constantly while a feed scrolls. Throttle with notificationTimeout and filter packages.

Side by side

UsageStatsManager AccessibilityService
Permission Usage access (Settings) Accessibility (Settings) + Play declaration
Latency Polling, about a second Instant
Sees inside apps No Yes, via the view tree
Can block Only by reacting after polling Yes
Best for Dashboards, daily totals Blocking apps and screens

What I ended up with

In Focsy, the accessibility service does the blocking: whole apps, parts of apps (Reels, Shorts, the TikTok feed) and websites. Usage stats power the dashboard, the daily goal and the experiments that show whether a change actually helped. Each API does the job it's good at.

If you're building something similar, I'm happy to answer questions in the comments. And if you want to see the result, Focsy is free on Google Play.