如何移除系统无障碍设置中我的应用对应的Accessibility Shortcut选项
Android 11+ 移除无障碍服务快捷方式开关方案
1. 修改服务配置隐藏快捷入口
Android 11(API 30)系统默认给所有无障碍服务开放了快捷方式入口,你可以通过新增配置项直接隐藏对应开关:
- 若使用XML文件声明无障碍服务配置(通常存放在
res/xml/目录下),在accessibility-service根标签中新增android:shortcutDisabled="true"属性即可,示例如下:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFeedbackType="feedbackGeneric" android:description="@string/accessibility_desc" <!-- 新增此行隐藏快捷方式入口 --> android:shortcutDisabled="true" android:canPerformGestures="true" ... />
- 若通过代码动态配置
AccessibilityServiceInfo,添加对应标记位即可,注意做版本兼容判断:
// Kotlin 示例 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { serviceInfo = serviceInfo.apply { flags = flags or AccessibilityServiceInfo.FLAG_SHORTCUT_DISABLED } }
// Java 示例 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { AccessibilityServiceInfo info = getServiceInfo(); info.flags |= AccessibilityServiceInfo.FLAG_SHORTCUT_DISABLED; setServiceInfo(info); }
注意:上述配置仅对Android 11(API 30)及以上系统生效,更低版本系统无内置无障碍快捷方式特性,无需额外兼容
2. 兜底拦截非法启停
为避免用户在应用更新前已经开启了快捷方式、仍能通过系统路径启停服务的情况,建议增加一层逻辑校验:
- 在应用内操作启停服务时,本地存储一个合法操作标记位
- 在无障碍服务的
onConnected()回调中先校验标记位,若标记位不合法,直接调用stopSelf()停止服务,同时弹出通知引导用户前往应用内操作 - 服务关闭时同步清空标记位,避免后续合法启动被拦截
内容的提问来源于stack exchange,提问作者poetryrocksalot
相关产品推荐
相关产品推荐

