1

I want to add progress and alert dialogs to whatever the active context may be, whenever the listener receives a message. With this code:

public class C2DMMessageReceiver extends BroadcastReceiver {
        @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    Log.w("C2DM", "Message Receiver called");
                    if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
                        Log.w("C2DM", "Received message");
                        final String payload = intent.getStringExtra("payload");
                        Log.d("C2DM", "dmControl: payload = " + payload);

                        // Message handling
                        if(payload.equals("DataUpdate")) {
                            progressDialog = ProgressDialog.show(context, "Please wait...", "Synchronizing data ...", true);
                syncData(context);
                progressDialog.dismiss();
                AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
                alertbox.setMessage("Data was updated");
                alertbox.create();
                alertbox.show();
                        }
                    }
                }
    }

I get the following error when i recieve message:

01-07 08:44:38.190: E/AndroidRuntime(750): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Trying to figure out what the best way to handle this would be, do I need to cache the active context in a singleton, and then access said singleton from the listener? Or is there a better way?

Thanks

RandomUser
  • 4,140
  • 17
  • 58
  • 94

1 Answers1

1

It's not possible to create/show a Dialog from a BroadcastReceiver. See the answer to this SO question android-broadcast-receiver-showing-a-dialog.

Also think carefully about doing this - as mentioned in the answer to that question, do you really want to annoy users by forcing a popup in their face when they're in the middle of doing something else?

The preferred way of handling C2DM messages would normally be to either process silently and invisibly or perhaps just creating a Notification that the user can act on later.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135