48

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

如果没有运行,我想提示用户AccessibilityService,但找不到这样做的好方法。是否有任何我可能缺少的 API 方法可以让我知道设备上启用了哪些可访问性服务?

4

8 回答 8

68

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

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;
}
于 2011-02-24T15:05:51.440 回答
64

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;
}

用法:

boolean enabled = isAccessibilityServiceEnabled(context, MyAccessibilityService.class);
于 2013-02-17T16:14:13.097 回答
34

检查服务是否启用

/**
 * 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;
}

用法:

boolean enabled = isAccessibilityServiceEnabled(context, MyAccessibilityService.class);

检测服务何时启用或禁用

进行回调:

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

订阅:

Uri uri = Settings.Secure.getUriFor(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
context.getContentResolver().registerContentObserver(uri, false, observer);

完成后退订:

context.getContentResolver().unregisterContentObserver(observer);

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

于 2016-11-12T22:07:56.597 回答
6

您可以跟踪服务是否像这样运行吗?如果启用了可访问性服务,它不应该也在运行吗?

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

 @Override
    public void onServiceConnected() {
      isEnabled = true; 
    }
 @Override 
    public void onDestroy(){ 
      isEnabled = false; 
    }
于 2014-11-24T15:09:10.767 回答
2

在您的活动服务刚刚启动时获取 ID。在所有初始化调用之后,在您的 Activity 服务 OnSeriviceCeonnected 中。用这个...

AccessibilityServiceInfo serviceInfo = this.getServiceInfo();
    String accessibilityId = serviceInfo.getId();

需要果冻豆 API

然后可以使用 Martin 的代码(isAccessibilityEnabled)来检查正在运行的服务。

于 2015-02-09T17:40:26.297 回答
0

也许为时已晚,但这是我检查无障碍服务状态的方法:

$ adb shell dumpsys accessibility

结果:

ACCESSIBILITY MANAGER (dumpsys accessibility)
User state[attributes:{id=0, currentUser=true, accessibilityEnabled=false, touchExplorationEnabled=false, displayMagnificationEnabled=false}
           services:{}]
于 2018-08-06T07:20:22.577 回答
-1
 AccessibilityManager accessibilityManager = (AccessibilityManager)context.getSystemService(Context.ACCESSIBILITY_SERVICE);
        List<AccessibilityServiceInfo> runningservice = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);

        accessibilityManager.addAccessibilityStateChangeListener(new AccessibilityManager.AccessibilityStateChangeListener() {
            @Override
            public void onAccessibilityStateChanged(boolean b) {
                Toast.makeText(MainActivity.this, "permission "+b, Toast.LENGTH_SHORT).show();
            }
        });

每当状态更改时都会调用 Listner 您可以跟踪布尔值以检查权限这是迄今为止检查权限的最简单和最轻的解决方案

于 2016-08-26T18:34:17.743 回答
-5

这需要更多次

AccessibilityManager am = (AccessibilityManager) context
        .getSystemService(Context.ACCESSIBILITY_SERVICE);
于 2014-02-23T10:30:27.187 回答