检测是否在 Android 辅助功能设置中启用了“高对比度”

new*_*v92 5 android accessibility

如何检测是否在辅助功能设置中启用了“高对比度”设置(适用于 Android 5.0+)?

alx*_*cms 5

在类中AccessibilityManager请参阅此处的源代码),您有一个名为的公共方法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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();
Run Code Online (Sandbox Code Playgroud)

  • 刚刚发现该方法是@hide方法,所以真的不应该能够像这样直接调用。需要通过反射的方式调用它。 (4认同)
  • 不知道为什么,但在我的例子中,isHighTextContrastEnabled() 没有为 AccessibilityManager 定义。 (2认同)