Below is the code which i am using to add a view
fun showPanel(context: Context) {
if (!AccessibilityUtils.checkAccessibilityEnabled()) {
LogUtils.d("no Accessibility permission")
showToast(context, "Please grant the permission.")
return
}
val windowManager by lazy { context.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
if (floatPanelView != null) {
LogUtils.d("floatPanelView has been initialized")
if (floatPanelView?.isAttachedToWindow == true) {
LogUtils.d("floatPanelView has been attached")
} else {
LogUtils.d("floatPanelView hasn't been attached")
windowManager.addView(floatPanelView, floatPanelView?.params)
}
return
}
LogUtils.d("init floatPanelView")
floatPanelView = FloatPanelView(context)
floatPanelView?.params =
WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY)
.apply {
format = PixelFormat.RGBA_8888
flags =
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
gravity = Gravity.START or Gravity.TOP
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
// Set it as 1/4 in vertical
y = ScreenUtils.getScreenHeight() / 4
}
LogUtils.d("params.y: ${floatPanelView?.params?.y}")
MainActivity.isServiceRunning.postValue(true)
windowManager.addView(floatPanelView, floatPanelView?.params)
}
and i am getting the below crashes in my app
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? android.view.ViewRootImpl.setView(ViewRootImpl.java:1274) android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409) android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109) com.iobits.tech.autotapper.presentation.views.MyWindowManager.showPanel(MyWindowManager.kt:96) com.iobits.tech.autotapper.service.FloatWindowService.onStartCommand(FloatWindowService.kt:52) android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4694) android.app.ActivityThread$H.handleMessage(ActivityThread.java:2226) android.os.Handler.dispatchMessage(Handler.java:106) android.os.Looper.loop(Looper.java:264)
The service class from where actually calling this is as below
class FloatWindowService : LifecycleAccessibilityService(), LifecycleEventObserver {
private val NOTIFICATION_ID = 1241
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
logd(intent?.action.toString())
logd("mStartService")
// Create a notification and start the service in the foreground
if (intent?.action == "STOP_SERVICE") {
logd("stopping service")
stopSelf()
stopForeground(true)
// disableSelf() to stop the service from settings
MyWindowManager.release(this)
// this.stopForeground(STOP_FOREGROUND_REMOVE) // Stop the service whe/n the stop button is clicked
return START_NOT_STICKY
}
if (Build.VERSION.SDK_INT >= 29) {
ServiceCompat.startForeground(
this,
NOTIFICATION_ID, createNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE
)
logd("mStartServices")
} else {
this.startForeground(NOTIFICATION_ID, createNotification())
}
if (intent?.hasExtra("isSingle") == true) {
try {
MyWindowManager.showPanelWithOneOnly(this)
} catch (e:Exception){
logd("service not yet registered")
}
} else {
try {
MyWindowManager.showPanel(this)
}catch (e:Exception){
logd("service not yet registered")
}
}
return super.onStartCommand(intent, flags, startId)
}