检测我的辅助功能服务是否已启用

时间:2013-08-07 04:58:48

标签: android service accessibility accessibilityservice

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

4 个答案:

答案 0 :(得分:58)

以下是检查您的辅助功能服务是否已启用的方法。

注意:使用您的服务更改YOURAccessibilityService的值。

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

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

答案 1 :(得分:1)

这是一个较小的版本,但是可以正常工作。

fun isAccessServiceEnabled(context: Context): Boolean {
    val prefString =
        Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
    return prefString.contains("${context.packageName}/${context.packageName}.${context.getString(R.string.access_service_name)}")
}

如果有任何缺失,请随时纠正我。

答案 2 :(得分:0)

这是Java Jakub Bláha's answer的修改版本。

public boolean isAccessServiceEnabled(Context context, Class accessibilityServiceClass)
{
    String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);

    return prefString!= null && prefString.contains(context.getPackageName() + "/" + accessibilityServiceClass.getName());
}

答案 3 :(得分:0)

Kotlin 版本基于@Pankaj Kumar 的回答

inline fun <reified T> Context.isAccessibilityEnabled(): Boolean {
    var enabled = 0
    try {
        enabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)
    } catch (e: SettingNotFoundException) {
        Timber.e(e)
    }
    if (enabled == 1) {
        val name = ComponentName(applicationContext, T::class.java)
        val services = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
        return services?.contains(name.flattenToString()) ?: false
    }
    return false
}
相关问题