Android:如何检查是否启用了特定的AccessibilityService

And*_*rew 39 android accessibility-api

我写过一个需要使用的Android应用程序AccessibilityService.我知道如何检查手机上是否启用了辅助功能,但我无法确定是否已在辅助功能菜单中专门启用了我的应用.

我想提示用户是否AccessibilityService没有运行,但找不到这样做的好方法.是否有任何我可能遗漏的API方法可以让我知道设备上启用了哪些辅助功能服务?

And*_*rew 65

我最终自己解决了这个问题:

    public boolean isAccessibilityEnabled(){
    int accessibilityEnabled = 0;
    final String LIGHTFLOW_ACCESSIBILITY_SERVICE = "com.example.test/com.example.text.ccessibilityService";
    boolean accessibilityFound = false;
    try {
        accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        Log.d(LOGTAG, "ACCESSIBILITY: " + accessibilityEnabled);
    } catch (SettingNotFoundException e) {
        Log.d(LOGTAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
    }

    TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

    if (accessibilityEnabled==1){
        Log.d(LOGTAG, "***ACCESSIBILIY IS ENABLED***: ");


         String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
         Log.d(LOGTAG, "Setting: " + settingValue);
         if (settingValue != null) {
             TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
             splitter.setString(settingValue);
             while (splitter.hasNext()) {
                 String accessabilityService = splitter.next();
                 Log.d(LOGTAG, "Setting: " + accessabilityService);
                 if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)){
                     Log.d(LOGTAG, "We've found the correct setting - accessibility is switched on!");
                     return true;
                 }
             }
         }

        Log.d(LOGTAG, "***END***");
    }
    else{
        Log.d(LOGTAG, "***ACCESSIBILIY IS DISABLED***");
    }
    return accessibilityFound;
}
Run Code Online (Sandbox Code Playgroud)

  • 我建议只使用分离器及以下代码.上面的代码是waaaaay太贵了.这就是我的应用程序太慢而且不知道原因的原因. (2认同)

小智 49

API级别14开始,还可以通过以下方式获取已启用的辅助功能服务AccessibilityManager:

public static boolean isAccessibilityServiceEnabled(Context context, Class<? extends AccessibilityService> service) {
    AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
    List<AccessibilityServiceInfo> enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);

    for (AccessibilityServiceInfo enabledService : enabledServices) {
        ServiceInfo enabledServiceInfo = enabledService.getResolveInfo().serviceInfo;
        if (enabledServiceInfo.packageName.equals(context.getPackageName()) && enabledServiceInfo.name.equals(service.getName()))
            return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

用法:

boolean enabled = isAccessibilityServiceEnabled(context, MyAccessibilityService.class);
Run Code Online (Sandbox Code Playgroud)

  • 实际上,这是行不通的,因为我可以看到我的可访问性服务已打开,但是`getEnabledAccessibilityServiceList()`返回一个空列表(在Pixel 2模拟器API 28上运行)。 (4认同)
  • 您的第一部分示例代码中存在拼写错误。getEnabledAccessibilityServiceList(AccessibilityEvent.TYPES_ALL_MASK) 应更改为 getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK) getEnabledAccessibilityServiceList() 的第二个参数是“feedbackTypeFlags”。 (2认同)

Sam*_*Sam 19

检查是否启用了服务

/**
 * Based on {@link com.android.settingslib.accessibility.AccessibilityUtils#getEnabledServicesFromSettings(Context,int)}
 * @see <a href="https://github.com/android/platform_frameworks_base/blob/d48e0d44f6676de6fd54fd8a017332edd6a9f096/packages/SettingsLib/src/com/android/settingslib/accessibility/AccessibilityUtils.java#L55">AccessibilityUtils</a>
 */
public static boolean isAccessibilityServiceEnabled(Context context, Class<?> accessibilityService) {
    ComponentName expectedComponentName = new ComponentName(context, accessibilityService);

    String enabledServicesSetting = Settings.Secure.getString(context.getContentResolver(),  Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
    if (enabledServicesSetting == null)
        return false;

    TextUtils.SimpleStringSplitter colonSplitter = new TextUtils.SimpleStringSplitter(':');
    colonSplitter.setString(enabledServicesSetting);

    while (colonSplitter.hasNext()) {
        String componentNameString = colonSplitter.next();
        ComponentName enabledService = ComponentName.unflattenFromString(componentNameString);

        if (enabledService != null && enabledService.equals(expectedComponentName))
            return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

用法:

boolean enabled = isAccessibilityServiceEnabled(context, MyAccessibilityService.class);
Run Code Online (Sandbox Code Playgroud)

检测时,该服务启用或禁用

进行回调:

ContentObserver observer = new ContentObserver() {
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        boolean accessibilityServiceEnabled = isAccessibilityServiceEnabled(context, MyAccessibilityService.class);
        //Do something here
    }
};
Run Code Online (Sandbox Code Playgroud)

订阅:

Uri uri = Settings.Secure.getUriFor(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
context.getContentResolver().registerContentObserver(uri, false, observer);
Run Code Online (Sandbox Code Playgroud)

完成后取消订阅:

context.getContentResolver().unregisterContentObserver(observer);
Run Code Online (Sandbox Code Playgroud)

请注意,这不适用于getEnabledAccessibilityServiceList()方法,因为其值与Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES值不同步.这就是为什么我认为使用Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES是一种更好的一刀切的方法.


use*_*940 6

你能跟踪这项服务是否正在运行吗?如果启用了辅助功能服务,它是否也应该运行?

public class MyAccessibilityService extends AccessibilityService{
public static boolean isEnabled = false; 

 @Override
    public void onServiceConnected() {
      isEnabled = true; 
    }
 @Override 
    public void onDestroy(){ 
      isEnabled = false; 
    }
Run Code Online (Sandbox Code Playgroud)