如何检测Android上的辅助功能设置已启用/禁用

时间:2016-05-24 20:06:06

标签: android

我对high contrast textcolor correctionmagnification设置特别感兴趣。我在网上做了一些研究,找不到我想要的东西。我看到一个关于检测high contrast text的答案:

AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();

但不知怎的,它给了我isHighTextContrastEnabled()的错误,说它是未定义的类型AccessibilityManager。

也未找到其他两种设置检测的解决方案。

4 个答案:

答案 0 :(得分:1)

    AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);

    Class clazz = am.getClass();
    Method m = null;
    try {
        m = clazz.getMethod("isHighTextContrastEnabled",null);
    } catch (NoSuchMethodException e) {
        Log.w("FAIL", "isHighTextContrastEnabled not found in AccessibilityManager");
    }


    Object result = null;
    try {
        result = m.invoke(am, null);
        if (result != null && result instanceof Boolean)  {
            Boolean b = (Boolean)result;
            Log.d("result", "b =" + b);
        }
    }  catch (Exception e) {
        android.util.Log.d("fail",  "isHighTextContrastEnabled invoked with an exception" + e.getMessage());
        return;
    }

我做测试,它返回false,所以它可以正常工作

答案 1 :(得分:1)

用它来进行色彩校正:

int color_correction_enabled = 0;

尝试{  color_correction_enabled = Settings.Secure.getInt(this.getContentResolver(),“accessibility_display_daltonizer_enabled”);

} catch(例外e){

color_correction_enabled = 0; //表示默认为false }

答案 2 :(得分:0)

做了什么

private fun checkForAcessibility(): Boolean {
    try {
        val accessibilityManager = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
        val accessibilityManagerClass = accessibilityManager.javaClass
        val isHighTextContrastEnabledMethod = accessibilityManagerClass.getMethod("isHighTextContrastEnabled")
        val result: Any = isHighTextContrastEnabledMethod.invoke(accessibilityManager) ?: return AccessibilityEnabledValue.ERROR_QUERYING_VALUE

        if (result !is Boolean) {
            return AccessibilityEnabledValue.ERROR_QUERYING_VALUE
        }

        return if (result) {
            AccessibilityEnabledValue.TRUE
        } else {
            AccessibilityEnabledValue.FALSE
        }
    } catch (e: Exception) {
        return AccessibilityEnabledValue.ERROR_QUERYING_VALUE
    }
}




enum class AccessibilityEnabledValue(val value: String) {
    TRUE("true"),
    FALSE("false"),
    ERROR_QUERYING_VALUE("error_querying_value")
}

我注意到isHighTextContrastEnabled()方法不包含参数。

答案 3 :(得分:0)

使用androidx.core:core-ktx的getSystemService扩展名,可能会被认为是草率的

isHighTextContrastEnabled = try {
    context.getSystemService<AccessibilityManager>()?.run {
        (javaClass.getMethod("isHighTextContrastEnabled").invoke(this) as? Boolean)
    } ?: false
} catch (e: Exception) {
    e.printStackTrace()
    false
}