如果启用了我的访问服务检测我的

由网友(我用十年换你一句好久不见)分享简介:我想知道我怎么能检测如果启用了自己的服务。所以,我可以检查是否未启用我的服务,然后告诉用户启用它。I was wondering how could I detect if my own service is enabled. So I could check if my service is not enabled...

我想知道我怎么能检测如果启用了自己的服务。所以,我可以检查是否未启用我的服务,然后告诉用户启用它。

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. 

注意:的更改值,最后弦乐服务=com.test.package.name/com.test.package.name.YOURAccessibilityService"你的服务。

Note : Change value of final String service = "com.test.package.name/com.test.package.name.YOURAccessibilityService" with your Service. 

// To check if service is enabled
private boolean isAccessibilitySettingsOn(Context mContext) {
        int accessibilityEnabled = 0;
        final String service = "com.test.package.name/com.test.package.name.YOURAccessibilityService";
        boolean accessibilityFound = false;
        try {
            accessibilityEnabled = Settings.Secure.getInt(
                    mContext.getApplicationContext().getContentResolver(),
                    android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
            Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
        } catch (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, "***ACCESSIBILIY IS ENABLED*** -----------------");
            String settingValue = Settings.Secure.getString(
                    mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
                splitter.setString(settingValue);
                while (splitter.hasNext()) {
                    String accessabilityService = splitter.next();

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

        return accessibilityFound;      
    }

并调用此方法的

And call this method as

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

这将检查并启动辅助功能设置,如果没有启用。

This will check and launch accessibility settings if not enabled.