我们知道我们可以通过三次点击手势检测放大率
Settings.Secure.getInt( getAppContext()?.getContentResolver(), "accessibility_display_magnification_enabled",0)
但是我们可以从 navBar 快捷方式和其他快捷方式中检测到它吗
试过
Settings.Secure.getInt( getAppContext()?.getContentResolver(), "accessibility_display_magnification_enabled",0)
&
Settings.Secure.getInt( getAppContext()?.getContentResolver(), "accessibility_display_magnification_scale",0)
但总是归零
是的,但要小心。我创建了一个
sharedFlow
来听缩放值:
private val magnificationScale: Float
get() =
Settings.Secure.getFloat(
contentResolver,
"accessibility_display_magnification_scale",
1.0f
)
private val magnifierFlow = MutableSharedFlow<Float>()
private val magnifierJob: Job
get() = GlobalScope.launch {
while (true) {
// you could only emit if the value is changed
magnifierFlow.emit(magnificationScale)
delay(250)
}
}
...
// in onResume() / onCreate()
magnifierJob.start()
...
magnifierFlow.collect { value ->
view.also { cb ->
runOnUiThread {
checkBox.isChecked = value != default
checkBox.setText("magnification: [${value}]")
}
}
}
据我所知,只有当用户使用不使用“临时”模式时,该值才会“保存”——(临时缩放是通过点击并按住完成的,而不是仅仅点击)——所以即使你可以检测到它,您将无法在不同的模式下检测到它。
我认为最大的问题归结为动机。由于此类数据可被视为健康数据,您绝对不希望将其用于分析,因为您需要应对许多法律合规性问题。
我发现了一篇关于检测屏幕阅读器的文章,他们列出了检测辅助技术是一种不好的做法的一些原因:
我知道我们能够检测 Android 上的屏幕阅读器,但那是因为我们可能需要为用户做这件事。由于用户的机制多种多样,它确实成为维护的噩梦。
但是如果真的有必要,我写了一些东西来帮助你:
val couldHaveMagnification: Boolean
get() {
val hasAccessibilityMagnificationButton = (Settings.Secure.getString(
contentResolver,
"accessibility_button_targets") ?: "")
.contains("com.android.server.accessibility.MagnificationController")
val hasAccessibilityMagnificationShortcut = (Settings.Secure.getString(
contentResolver,
"accessibility_shortcut_target_service") ?: "")
.contains("com.android.server.accessibility.MagnificationController")
val hasTripleTapToZoomOn = Settings.Secure.getInt(
contentResolver,
"accessibility_display_magnification_enabled", 0) == 1
return hasAccessibilityMagnificationButton || hasAccessibilityMagnificationShortcut || hasTripleTapToZoomOn
}
请注意,这仅在 Pixel 7 手机上进行过测试。