【问题标题】:Detect if 'High contrast' is enabled in Android accessibility settings检测是否在 Android 辅助功能设置中启用了“高对比度”
【发布时间】:2021-01-04 21:01:43
【问题描述】:

如何检测无障碍设置中是否启用了“高对比度”设置(适用于 Android 5.0+)?

【问题讨论】:

    标签: android accessibility


    【解决方案1】:

    AccessibilityManager 类 (see source here) 中,您有一个名为 isHighTextContrastEnabled 的公共方法,您可以使用它来获取您的信息:

    /**
     * Returns if the high text contrast in the system is enabled.
     * <p>
     * <strong>Note:</strong> You need to query this only if you application is
     * doing its own rendering and does not rely on the platform rendering pipeline.
     * </p>
     *
     * @return True if high text contrast is enabled, false otherwise.
     *
     * @hide
     */
    public boolean isHighTextContrastEnabled() {
        synchronized (mLock) {
            IAccessibilityManager service = getServiceLocked();
            if (service == null) {
                return false;
            }
            return mIsHighTextContrastEnabled;
        }
    }
    

    因此,在您的代码中,您可以通过这样做来访问此方法(如果您在 Activity 中):

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

    【讨论】:

    • 不知道为什么,但在我的情况下,AccessibilityManager 的 isHighTextContrastEnabled() 是未定义的。
    • 刚刚发现方法是@hide方法,所以真的不应该这样直接调用。需要通过反射的方式调用。
    • 这对我帮助不大。
    【解决方案2】:
    • @alxscms 的回答可能是正确的,但它对我没有帮助所以我找到了一种 替代方法 来检查 高对比度文本 在 android 中是否启用。李>

    如果在用户手机中启用了HighContrastText,以下函数将返回true,否则返回false。

    以下功能已在所有 android 手机中检查,并且可以正常工作。

    public static boolean isHighContrastTextEnabled(Context context) {
        if (context != null) {
            AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
            Method m = null;
    
            if (am != null) {
                try {
                    m = am.getClass().getMethod("isHighTextContrastEnabled", null);
                } catch (NoSuchMethodException e) {
                    Log.i("FAIL", "isHighTextContrastEnabled not found in AccessibilityManager");
                }
            }
    
            Object result;
            if (m != null) {
                try {
                    result = m.invoke(am, null);
                    if (result instanceof Boolean) {
                        return (Boolean) result;
                    }
                } catch (Exception e) {
                    Log.i("fail", "isHighTextContrastEnabled invoked with an exception" + e.getMessage());
                }
            }
        }
        return false;
    }
    

    我希望这可以帮助更多人。

    【讨论】:

      猜你喜欢
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 2022-01-12
      • 2022-07-16
      相关资源
      最近更新 更多