检测我的无障碍服务是否已启用 [英] Detect if my accessibility service is enabled

查看:60
本文介绍了检测我的无障碍服务是否已启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何检测我自己的服务是否已启用.所以我可以检查我的服务是否未启用,然后告诉用户启用它.

I was wondering how could I detect if my own service is enabled. So I could check if my service is not enabled, then tell the user to enable it.

推荐答案

以下是检查您的无障碍服务是否启用的方法.

Below is the method to check if your accessibility service is enabled or not. 

注意:随您的服务更改 YOURAccessibilityService 的值.

Note: Change value of YOURAccessibilityService with your Service. 

// To check if service is enabled
private boolean isAccessibilitySettingsOn(Context mContext) {
    int accessibilityEnabled = 0;
    final String service = getPackageName() + "/" + YOURAccessibilityService.class.getCanonicalName();
    try {
        accessibilityEnabled = Settings.Secure.getInt(
                mContext.getApplicationContext().getContentResolver(),
                android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
    } catch (Settings.SettingNotFoundException e) {
        Log.e(TAG, "Error finding setting, default accessibility to not found: "
                + e.getMessage());
    }
    TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

    if (accessibilityEnabled == 1) {
        Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
        String settingValue = Settings.Secure.getString(
                mContext.getApplicationContext().getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        if (settingValue != null) {
            mStringColonSplitter.setString(settingValue);
            while (mStringColonSplitter.hasNext()) {
                String accessibilityService = mStringColonSplitter.next();

                Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
                if (accessibilityService.equalsIgnoreCase(service)) {
                    Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
                    return true;
                }
            }
        }
    } else {
        Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
    }

    return false;
}

并调用此方法:

if (!isAccessibilitySettingsOn(getApplicationContext())) {
    startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}

如果未启用,这将检查并启动辅助功能设置.

This will check and launch accessibility settings if not enabled.

这篇关于检测我的无障碍服务是否已启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆