AlertDialog in BroadcastReceiver

2019-01-20 12:49发布

问题:

I'm trying to create an application that can use the android as a fax machine, IE Send a picture as a fax or receive a fax and save as a picture. So far I'm starting from the ground up and making sure I can intercept a call at the users discretion. I have an Receiver registered in the Manifest of my program with a filter of Phone_State which flags when the state has changed(IE incoming call).

So on my BroadcastReceiver I'm trying to have an AlertDialog popup prompting the user to either accept as fax or call but the AlertDialog seems to throw a android.view.WindowManager$BadTokenException Error when it has an incoming call. My code is just simple an onReceive(context arg0, intent arg1) and I pass the arg0 to the AlertDialog...

The full error message is below

08-08 00:16:14.315: ERROR/AndroidRuntime(275): java.lang.RuntimeException: Unable to start receiver com.android.fax.IncomingFax: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-08 00:16:14.315: ERROR/AndroidRuntime(275):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2550)
08-08 00:16:14.315: ERROR/AndroidRuntime(275):     at android.app.ActivityThread.access$3000(ActivityThread.java:116)
08-08 00:16:14.315: ERROR/AndroidRuntime(275):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1843)

From what I have seen in the AlertDialog code, it passes the context as well as a Window and WindowManager, which I believe is why it's crashing, is there a better way or something else I should be using which might overlay the call screen?

回答1:

First, you can't get to the in-call audio stream from Android. With that limitation, I have no idea how you are going to receive a fax.

With respect to your error, a manifest-registered BroadcastReceiver is not an Activity, and so it cannot create a dialog. You could, in principle, call startActivity() to start up an Activity (perhaps one themed like a dialog), but I do not know whether or not it would appear over the in-call screen.