I have dealt with a couple of bugs which have driven me nuts for almost a year now.
I am using Android's Accessibility Services to perform automated tasks, tapping on buttons, issuing custom gestures and displaying feedback on the screen through Toasts.
For most of the cases, it works like a charm. But I have run through at least two annoying bugs I cannot really solve at the moment.
The first one:
Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@5ff054e is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:467)
at android.widget.Toast$TN$2.handleMessage(Toast.java:350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6138)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
So I’ve made a little bit of research and I have come up with this resource: http://dimitar.me/android-displaying-dialogs-from-background-threads where we are stated that we need to synchronously check that the activity is not finishing before we actually display some Toast / Dialog on top of it.
Thing is: as I am displaying on top of another app's activity, I naturally got no control over it. And sometimes I got the Traceback above without anymore information, resulting in a crash. I cannot even handle the exception.
I have come up with additional clues there: android.view.WindowManager$BadTokenException: Unable to add window -- at Toast
We are told that the issue is well known and fixed after Android 8.0. It's fine, I only got these bugs through Android 7.0 anyway.
And now I have a second issue, targetting Android 9 (maybe other versions) and I cannot really reproduce them:
Fatal Exception: java.lang.IllegalStateException: View android.widget.LinearLayout{59b180e V.E...... ......ID 0,0-640,296} has already been added to the window manager.
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:328)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:499)
at android.widget.Toast$TN$1.handleMessage(Toast.java:403)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
I am pretty sure that this is also due to the fact that I am displaying some Toast on top of an activity which I do not control.
My question is: Are these bugs due to Android or am I doing anything wrong anywhere? I am not sure that showing code on my end is relevant, as there is nothing coming from my own code from the Tracebacks I got.
More generally: is it okay to display some Toasts on top of activity / apps you do not control?
What would you guys suggest?