【发布时间】:2021-01-04 21:01:43
【问题描述】:
如何检测无障碍设置中是否启用了“高对比度”设置(适用于 Android 5.0+)?
【问题讨论】:
标签: android accessibility
如何检测无障碍设置中是否启用了“高对比度”设置(适用于 Android 5.0+)?
【问题讨论】:
标签: android accessibility
在 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();
【讨论】:
如果在用户手机中启用了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;
}
我希望这可以帮助更多人。
【讨论】: