如何在Android中检测无障碍快捷按钮是否激活(无需启用无障碍服务)
如何在未激活无障碍服务时检测Android无障碍快捷按钮的激活状态

以下是两种无需激活自身无障碍服务的可行检测方案:
方案一:读取系统设置字段(推荐,兼容性较好)
Android 8.0(API 26)及以上系统提供了直接的设置字段判断无障碍快捷功能是否开启,低版本可结合系统服务列表辅助验证:
代码实现(Kotlin)
import android.content.Context import android.provider.Settings import android.view.accessibility.AccessibilityManager fun checkAccessibilityShortcutStatus(context: Context): Boolean { // 核心逻辑:读取系统无障碍快捷开关状态 val isShortcutEnabled = Settings.Secure.getInt( context.contentResolver, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 0 ) == 1 // 低版本兼容:检测系统是否启用了无障碍快捷服务 if (!isShortcutEnabled && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) { val accessibilityManager = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager val enabledServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityManager.FEEDBACK_ALL_MASK) return enabledServices.any { it.packageName.startsWith("com.android.") && it.resolveInfo.serviceInfo.name.contains("Shortcut") } } return isShortcutEnabled }
权限要求
需要在AndroidManifest.xml中添加系统级权限:
<uses-permission android:name="android.permission.READ_SECURE_SETTINGS" />
普通APP需通过ADB命令授予该权限:
adb shell pm grant 你的应用包名 android.permission.READ_SECURE_SETTINGS
方案二:检测特定无障碍服务的快捷按钮状态
如果需要判断某款特定APP的无障碍服务快捷按钮是否激活,可读取系统保存的快捷目标列表:
代码实现
import android.content.Context import android.provider.Settings fun isSpecificServiceShortcutActive(context: Context, serviceComponent: String): Boolean { val activeShortcuts = Settings.Secure.getString( context.contentResolver, Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGETS ) return activeShortcuts?.contains(serviceComponent) == true }
使用时传入目标服务的组件名(例如"com.example.myapp/.MyAccessibilityService")。
内容的提问来源于stack exchange,提问作者AmirMohamamd
相关产品推荐
相关产品推荐

