检测是否在 Android 辅助功能设置中启用了 'High contrast'
Detect if 'High contrast' is enabled in Android accessibility settings
如何检测辅助功能设置中是否启用了 'High Contrast' 设置(在 Android 5.0+ 上可用)?
在 AccessibilityManager
class (see source here) 中,您有一个名为 isHighTextContrastEnabled
的 public 方法,您可以使用它来获取信息:
/**
* 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();
- @alxscms 的回答可能是正确的,但对我没有帮助所以我找到了一种替代方法来检查高对比度文本是在 android.
中是否启用
Below function will return true if HighContrastText is enabled in user phone and otherwise return false.
Below function is checked in all android phones and it's working.
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;
}
I hope this can help many more others.
我们可以像这样检查高对比度字体
public boolean isHighTextContrastEnabled(Context context) {
return Settings.Secure.getInt(context.getContentResolver(), "high_text_contrast_enabled", 0) == 1;
}
如何检测辅助功能设置中是否启用了 'High Contrast' 设置(在 Android 5.0+ 上可用)?
在 AccessibilityManager
class (see source here) 中,您有一个名为 isHighTextContrastEnabled
的 public 方法,您可以使用它来获取信息:
/**
* 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();
- @alxscms 的回答可能是正确的,但对我没有帮助所以我找到了一种替代方法来检查高对比度文本是在 android. 中是否启用
Below function will return true if HighContrastText is enabled in user phone and otherwise return false.
Below function is checked in all android phones and it's working.
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;
}
I hope this can help many more others.
我们可以像这样检查高对比度字体
public boolean isHighTextContrastEnabled(Context context) {
return Settings.Secure.getInt(context.getContentResolver(), "high_text_contrast_enabled", 0) == 1;
}