我正在创建一个 android 应用程序,我必须实现一个功能,我的应用程序具有“在其他应用程序上显示”权限,并且从辅助功能服务它需要在其他应用程序上显示一个透明的相机覆盖。
需要什么?
WhatsApp 打开后,立即从我的应用程序中在 WhatsApp 应用程序上显示透明相机叠加层。附上截图。
我做了什么
1-添加显示在应用程序权限工作
2- 添加了无障碍服务,只要打开 WhatsApp,就会触发 onAccessibilityEvent。
WhatsApp打开时我尝试添加的相机覆盖
1-我添加了此处提到的捕获预览类
2-尝试使用 cameraX Preview 但没有运气
下面是我想要开始透明相机覆盖的 AccessibilityService 代码
class CameraAccessibilityService : AccessibilityService() {
val tinyDB: TinyDB by inject()
override fun onInterrupt() {}
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
if (event?.eventType?.equals(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)!! || event?.eventType?.equals(
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
)!!
) {
if (event.packageName == Constants.WHATSAPP_PKG_NAME || event.packageName == Constants.WHATSAPP_BUSINESS_PKG_NAME) {
if (tinyDB.getBoolean(Constants.isCameraOverLayEnabled)) {
Toast.makeText(applicationContext, "Setting ON", Toast.LENGTH_SHORT)
.show()
val preview=CapturePreview(this)
val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = DisplayMetrics()
wm.defaultDisplay.getMetrics(metrics)
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, // TYPE_SYSTEM_ALERT is denied in apiLevel >=19
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT
)
params.title = "Touhou"
wm.addView(preview, params)
} else {
Toast.makeText(applicationContext, "Setting OFF", Toast.LENGTH_SHORT)
.show()
}
}
}
}
override fun onServiceConnected() {
this.serviceInfo.apply {
eventTypes =
AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED or AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
packageNames =
arrayOf(Constants.WHATSAPP_PKG_NAME, Constants.WHATSAPP_BUSINESS_PKG_NAME)
feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK
notificationTimeout = 100
}
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "Save Status CameraAccessibilityService Destroyed")
}
}
有人可以帮我解决这个问题。任何帮助将不胜感激。