Android开发:如何检测设备当前是否开启灰度显示模式?
检测设备是否开启色彩校正的灰度显示模式
Android平台实现
API 26及以上版本
直接使用AccessibilityManager提供的API获取色彩校正状态,若需精准校验是否为灰度模式,需结合设置项类型判断:
// 获取色彩校正总开关状态 AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE); boolean isColorCorrectionEnabled = accessibilityManager.isColorCorrectionEnabled(); // 校验是否为灰度模式(0对应灰度类型) ContentResolver contentResolver = getContentResolver(); int daltonizerType = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_TYPE, -1); boolean isGreyscaleEnabled = isColorCorrectionEnabled && daltonizerType == 0;
API 26以下版本
通过系统设置的内容提供者直接查询完整状态:
ContentResolver contentResolver = getContentResolver(); int daltonizerEnabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0); int daltonizerType = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_TYPE, -1); boolean isGreyscaleEnabled = daltonizerEnabled == 1 && daltonizerType == 0;
iOS平台实现
使用UIKit框架中UIAccessibility的标准属性直接获取灰度显示状态:
Swift版本
import UIKit let isGreyscaleEnabled = UIAccessibility.isGrayscaleEnabled
Objective-C版本
#import <UIKit/UIKit.h> BOOL isGreyscaleEnabled = UIAccessibilityIsGrayscaleEnabled();
注:该API从iOS 10开始可用,官方推荐优先使用此标准接口,无需额外权限。
内容的提问来源于stack exchange,提问作者user2782067
相关产品推荐
相关产品推荐

