I've been working on OTA support for PhoneLab testbed. And one problem I encountered is that, when I tried to pop out an AlertDialog to let user confirm update, I get this error that saied something like this:
android.view.WindowManager$BadTokenException: Unable to add window -- token null
is not for an application
Apparently, the context
I used to create the dialog, which is the service
context, is not valid in the sense
that it has not windows attached. Yet create an Activity
just to pop out a
alert dialog is a bit of overdone, since my app is basically a background
service.
Here is how I solved this problem.
- Add
android.permission.SYSTEM_ALERT_WINDOW
permission toAndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
- After creating the dialog, before show it, set its window type to system alert.
// builder set up code here
// ...
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();