{"id":7442,"date":"2026-11-28T02:00:57","date_gmt":"2026-11-28T05:00:57","guid":{"rendered":"https:\/\/tucumandevelopers.com\/index.php\/2026\/11\/28\/how-i-built-a-reels-and-shorts-blocker-for-android-with-accessibilityservice\/"},"modified":"2026-11-28T02:00:57","modified_gmt":"2026-11-28T05:00:57","slug":"how-i-built-a-reels-and-shorts-blocker-for-android-with-accessibilityservice","status":"publish","type":"post","link":"https:\/\/tucumandevelopers.com\/index.php\/2026\/11\/28\/how-i-built-a-reels-and-shorts-blocker-for-android-with-accessibilityservice\/","title":{"rendered":"How I built a Reels and Shorts blocker for Android with AccessibilityService"},"content":{"rendered":"<div>\n<div><\/div>\n<p>And the config in <code>res\/xml\/accessibility_config.xml<\/code>: <\/p>\n<div>\n<pre><code><span>&lt;accessibility-service<\/span> <span>xmlns:android<\/span><span>=<\/span><span>\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span> <span>android:accessibilityEventTypes<\/span><span>=<\/span><span>\"typeWindowStateChanged|typeWindowContentChanged\"<\/span> <span>android:accessibilityFeedbackType<\/span><span>=<\/span><span>\"feedbackGeneric\"<\/span> <span>android:canRetrieveWindowContent<\/span><span>=<\/span><span>\"true\"<\/span> <span>android:notificationTimeout<\/span><span>=<\/span><span>\"300\"<\/span> <span>android:description<\/span><span>=<\/span><span>\"@string\/accessibility_description\"<\/span> <span>\/&gt;<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p><code>notificationTimeout<\/code> matters more than it looks. Feeds fire content-change events constantly while you scroll. Without throttling, the service does far too much work and drains the battery.<\/p>\n<p>It also helps to narrow which apps the service listens to. <code>AccessibilityServiceInfo.packageNames<\/code> can be updated at runtime with <code>setServiceInfo()<\/code>, so the service only receives events from apps that actually have a rule.<\/p>\n<h2> <a name=\"blocking-a-screen\" href=\"#blocking-a-screen\"> <\/a> Blocking a screen <\/h2>\n<p>Simplified, the core loop looks like this: <\/p>\n<div>\n<pre><code><span>class<\/span> <span>BlockerAccessibilityService<\/span> <span>:<\/span> <span>AccessibilityService<\/span><span>(<\/span><span>)<\/span> <span>{<\/span> <span>override<\/span> <span>fun<\/span> <span>onAccessibilityEvent<\/span><span>(<\/span><span>event<\/span><span>:<\/span> <span>AccessibilityEvent<\/span><span>)<\/span> <span>{<\/span> <span>val<\/span> <span>pkg<\/span> <span>=<\/span> <span>event<\/span><span>.<\/span><span>packageName<\/span><span>?<\/span><span>.<\/span><span>toString<\/span><span>(<\/span><span>)<\/span> <span>?:<\/span> <span>return<\/span> <span>val<\/span> <span>rule<\/span> <span>=<\/span> <span>rules<\/span><span>.<\/span><span>forPackage<\/span><span>(<\/span><span>pkg<\/span><span>)<\/span> <span>?:<\/span> <span>return<\/span> <span>val<\/span> <span>shouldBlock<\/span> <span>=<\/span> <span>when<\/span> <span>{<\/span> <span>rule<\/span><span>.<\/span><span>blocksWholeApp<\/span><span>(<\/span><span>)<\/span> <span>-&gt;<\/span> <span>rule<\/span><span>.<\/span><span>isActiveNow<\/span><span>(<\/span><span>)<\/span> <span>rule<\/span><span>.<\/span><span>blocksShortVideoFeed<\/span><span>(<\/span><span>)<\/span> <span>-&gt;<\/span> <span>isShortVideoFeed<\/span><span>(<\/span><span>pkg<\/span><span>,<\/span> <span>rootInActiveWindow<\/span><span>)<\/span> <span>else<\/span> <span>-&gt;<\/span> <span>false<\/span> <span>}<\/span> <span>if<\/span> <span>(<\/span><span>shouldBlock<\/span><span>)<\/span> <span>block<\/span><span>(<\/span><span>pkg<\/span><span>)<\/span> <span>}<\/span> <span>private<\/span> <span>fun<\/span> <span>block<\/span><span>(<\/span><span>pkg<\/span><span>:<\/span> <span>String<\/span><span>)<\/span> <span>{<\/span> <span>startActivity<\/span><span>(<\/span> <span>Intent<\/span><span>(<\/span><span>this<\/span><span>,<\/span> <span>BlockActivity<\/span><span>::<\/span><span>class<\/span><span>.<\/span><span>java<\/span><span>)<\/span> <span>.<\/span><span>putExtra<\/span><span>(<\/span><span>\"blocked_package\"<\/span><span>,<\/span> <span>pkg<\/span><span>)<\/span> <span>.<\/span><span>addFlags<\/span><span>(<\/span><span>Intent<\/span><span>.<\/span><span>FLAG_ACTIVITY_NEW_TASK<\/span><span>)<\/span> <span>)<\/span> <span>performGlobalAction<\/span><span>(<\/span><span>GLOBAL_ACTION_HOME<\/span><span>)<\/span> <span>}<\/span> <span>override<\/span> <span>fun<\/span> <span>onInterrupt<\/span><span>(<\/span><span>)<\/span> <span>=<\/span> <span>Unit<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p><code>isActiveNow()<\/code> covers the three kinds of whole-app rules: always on, on a schedule, or after a daily time limit.<\/p>\n<h2> <a name=\"telling-reels-apart-from-the-rest-of-instagram\" href=\"#telling-reels-apart-from-the-rest-of-instagram\"> <\/a> Telling Reels apart from the rest of Instagram <\/h2>\n<p>This is the hard part, and the part that keeps breaking. <code>rootInActiveWindow<\/code> gives you the view tree of the app in front, and you look for signals that you&#8217;re on the short-video screen. <\/p>\n<div>\n<pre><code><span>private<\/span> <span>fun<\/span> <span>isShortVideoFeed<\/span><span>(<\/span><span>pkg<\/span><span>:<\/span> <span>String<\/span><span>,<\/span> <span>root<\/span><span>:<\/span> <span>AccessibilityNodeInfo<\/span><span>?<\/span><span>)<\/span><span>:<\/span> <span>Boolean<\/span> <span>{<\/span> <span>root<\/span> <span>?:<\/span> <span>return<\/span> <span>false<\/span> <span>return<\/span> <span>detectors<\/span><span>[<\/span><span>pkg<\/span><span>]<\/span><span>?<\/span><span>.<\/span><span>matches<\/span><span>(<\/span><span>root<\/span><span>)<\/span> <span>==<\/span> <span>true<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>I won&#8217;t list the exact signals, because they change all the time. But the principles hold:<\/p>\n<ul>\n<li> <strong>Never rely on one signal.<\/strong> Instagram, YouTube and Facebook change their layouts often, sometimes in A\/B tests that only some users see. Matching several signals together survives small redesigns.<\/li>\n<li> <strong>Keep detection rules easy to update.<\/strong> You will be shipping fixes for other apps&#8217; redesigns.<\/li>\n<li> <strong>Test across app versions,<\/strong> not just the latest. Users don&#8217;t all update at once.<\/li>\n<li> <strong>Treat every surface separately.<\/strong> A Reel can open from the Reels tab, from Explore, or from a link in DMs. Each one is its own case.<\/li>\n<\/ul>\n<h2> <a name=\"blocking-websites-subdomains-and-renamed-sites\" href=\"#blocking-websites-subdomains-and-renamed-sites\"> <\/a> Blocking websites, subdomains and renamed sites <\/h2>\n<p>For websites, the service reads the address bar of supported browsers. In Chrome, that&#8217;s the node with the view ID <code>com.android.chrome:id\/url_bar<\/code>. Other browsers use their own IDs, so each needs its own entry.<\/p>\n<p>Matching needs two details people forget: subdomains, and sites that change their name. <\/p>\n<div>\n<pre><code><span>private<\/span> <span>val<\/span> <span>aliases<\/span> <span>=<\/span> <span>mapOf<\/span><span>(<\/span> <span>\"twitter.com\"<\/span> <span>to<\/span> <span>setOf<\/span><span>(<\/span><span>\"x.com\"<\/span><span>)<\/span><span>,<\/span> <span>\"x.com\"<\/span> <span>to<\/span> <span>setOf<\/span><span>(<\/span><span>\"twitter.com\"<\/span><span>)<\/span><span>,<\/span> <span>)<\/span> <span>fun<\/span> <span>isBlocked<\/span><span>(<\/span><span>host<\/span><span>:<\/span> <span>String<\/span><span>,<\/span> <span>blocked<\/span><span>:<\/span> <span>Set<\/span><span>&lt;<\/span><span>String<\/span><span>&gt;<\/span><span>)<\/span><span>:<\/span> <span>Boolean<\/span> <span>{<\/span> <span>val<\/span> <span>h<\/span> <span>=<\/span> <span>host<\/span><span>.<\/span><span>lowercase<\/span><span>(<\/span><span>)<\/span><span>.<\/span><span>removePrefix<\/span><span>(<\/span><span>\"www.\"<\/span><span>)<\/span> <span>val<\/span> <span>domains<\/span> <span>=<\/span> <span>blocked<\/span><span>.<\/span><span>flatMap<\/span> <span>{<\/span> <span>setOf<\/span><span>(<\/span><span>it<\/span><span>)<\/span> <span>+<\/span> <span>aliases<\/span><span>[<\/span><span>it<\/span><span>]<\/span><span>.<\/span><span>orEmpty<\/span><span>(<\/span><span>)<\/span> <span>}<\/span> <span>return<\/span> <span>domains<\/span><span>.<\/span><span>any<\/span> <span>{<\/span> <span>d<\/span> <span>-&gt;<\/span> <span>h<\/span> <span>==<\/span> <span>d<\/span> <span>||<\/span> <span>h<\/span><span>.<\/span><span>endsWith<\/span><span>(<\/span><span>\".$d\"<\/span><span>)<\/span> <span>}<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Block <code>reddit.com<\/code> and <code>old.reddit.com<\/code> is blocked too. Block <code>twitter.com<\/code> and <code>x.com<\/code> is blocked too.<\/p>\n<h2> <a name=\"usage-stats-from-androids-own-data\" href=\"#usage-stats-from-androids-own-data\"> <\/a> Usage stats from Android&#8217;s own data <\/h2>\n<p>For the dashboard and the daily goal, Focsy reads Android&#8217;s own usage data through <code>UsageStatsManager<\/code>. The user grants Usage access in Settings, and the app doesn&#8217;t have to track anything itself. <\/p>\n<div>\n<pre><code><span>val<\/span> <span>usm<\/span> <span>=<\/span> <span>getSystemService<\/span><span>(<\/span><span>UsageStatsManager<\/span><span>::<\/span><span>class<\/span><span>.<\/span><span>java<\/span><span>)<\/span> <span>val<\/span> <span>stats<\/span> <span>=<\/span> <span>usm<\/span><span>.<\/span><span>queryAndAggregateUsageStats<\/span><span>(<\/span><span>startOfDay<\/span><span>,<\/span> <span>System<\/span><span>.<\/span><span>currentTimeMillis<\/span><span>(<\/span><span>)<\/span><span>)<\/span> <span>val<\/span> <span>minutesByApp<\/span> <span>=<\/span> <span>stats<\/span><span>.<\/span><span>mapValues<\/span> <span>{<\/span> <span>it<\/span><span>.<\/span><span>value<\/span><span>.<\/span><span>totalTimeInForeground<\/span> <span>\/<\/span> <span>60_000<\/span> <span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<h2> <a name=\"the-problems-nobody-warns-you-about\" href=\"#the-problems-nobody-warns-you-about\"> <\/a> The problems nobody warns you about <\/h2>\n<ul>\n<li> <strong>Phone makers kill background services.<\/strong> Xiaomi, Huawei, Samsung and others have aggressive battery managers that can stop an accessibility service. Your onboarding needs to send users to the right battery settings. <a href=\"https:\/\/dontkillmyapp.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">dontkillmyapp.com<\/a> is the best reference for each brand.<\/li>\n<li> <strong>Users can always turn the service off.<\/strong> Android lets anyone disable an accessibility service in Settings, and it should. So &#8220;hard to turn off&#8221; has to come from the app itself. In Focsy that&#8217;s Strict Mode, where you complete a challenge before blocking turns off and every disable is counted, and friend approval, where a friend has to accept your unlock request.<\/li>\n<li> <strong>Performance.<\/strong> Content-change events arrive many times per second while scrolling. Throttle them, skip packages without rules, and keep the view-tree walk short.<\/li>\n<\/ul>\n<h2> <a name=\"try-it-or-tell-me-what-breaks\" href=\"#try-it-or-tell-me-what-breaks\"> <\/a> Try it, or tell me what breaks <\/h2>\n<p>Focsy is free on <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.focsy&amp;utm_source=devto&amp;utm_campaign=build_post\" target=\"_blank\" rel=\"noopener noreferrer\">Google Play<\/a>, and blocking Reels and Shorts is free with no limit. If you&#8217;re building something with <code>AccessibilityService<\/code>, ask me anything in the comments. And if Focsy misses a Reels or Shorts screen on your phone, tell me your phone model and app version. That&#8217;s the most useful bug report I can get.<\/p>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Fuente: <a href=\"https:\/\/dev.to\/focsy\/how-i-built-a-reels-and-shorts-blocker-for-android-with-accessibilityservice-1eo2\">Art\u00edculo original<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>And the config in res\/xml\/accessibility_config.xml: &lt;accessibility-service xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221; android:accessibilityEventTypes=&#8221;typeWindowStateChanged|typeWindowContentChanged&#8221; android:accessibilityFeedbackType=&#8221;feedbackGeneric&#8221; android:canRetrieveWindowContent=&#8221;true&#8221; android:notificationTimeout=&#8221;300&#8243; android:description=&#8221;@string\/accessibility_description&#8221; \/&gt; notificationTimeout matters more than it looks. Feeds fire content-change events constantly while you scroll. Without throttling, the service does far too much work and drains the battery. It also helps to narrow which apps the service listens to. AccessibilityServiceInfo.packageNames can be updated [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7441,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"webixso_pending_account_ids":""},"categories":[41],"tags":[],"class_list":["post-7442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devto"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/7442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/comments?post=7442"}],"version-history":[{"count":0,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/7442\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media\/7441"}],"wp:attachment":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media?parent=7442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/categories?post=7442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/tags?post=7442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}