class MyServiceClass : AccessibilityService() {
private val overlayParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY, overlayFlags,
PixelFormat.TRANSLUCENT
).apply {
gravity = Gravity.TOP
}
override fun onServiceConnected() {
val windowManager = baseContext.getSystemService(WINDOW_SERVICE) as WindowManager
val overlayLayout = FrameLayout(context)
windowManager.addView(overlayLayout, overlayParams)//here I have the exception
}
}
I have a service class which extends AccessibilityService.
There, onServiceConnected callback, I try to add a view to window manager and I have the following exception:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
What could be the reason? Is it that I am adding too early, or could the service got killed while triggering onServiceConnected?