In my project, when the service class gets called, it overlays an Image on top of the mobile screen. Foreground service is used to continuously keep the image on top of the mobile screen even when my app gets closed.
The only issue left to resolve here is that: This overlaying image is not allowing the touch events to pass through the image to the mobile screen and the issue is only occurring on Android 11 and 12. I have tested the code on Android 6,10 and 13. it is working fine.
How can I modify this code so that touch events can be allowed to pass through the overlaying image? I have also successfully asked the run-time permission for 'Settings.ACTION_MANAGE_OVERLAY_PERMISSION' to overlay my image on top of mobile screen
So this is the function that overlays the image on top of my mobile screen. It is working fine on different versions of Android but the issue persists only on Android 11 and 12. How can I make the touch pass through the image on android 11 and 12?
private fun showImageOverlay(imageUri: Uri) {
removeImageOverlay()
val type: Int = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
} else {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
}
val flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
val layoutParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
type,
flags,
PixelFormat.TRANSLUCENT
)
val overlayView = ImageView(this).apply {
// Use Glide to load and set the image
Glide.with(this@BrokenServiceClass)
.load(imageUri)
.into(this)
}
val touchInterceptor = View.OnTouchListener { _, _ -> // Pass touch events to the underlying elements
false
}
overlayView.setOnTouchListener(touchInterceptor)
windowManager?.addView(overlayView, layoutParams)
overlayView.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) {
// The overlay view is attached to the window
}
override fun onViewDetachedFromWindow(v: View) {
// The overlay view is detached from the window
removeImageOverlay()
}
})
}