1

I have a broadcast receiver that detects the end of a outgoing call. After that, i want to show some dialog in my activity. I tried with this method in my activity:

 public static void buildShouldIFollowMessage(String callLength) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setMessage(Constants.CONN_TIMEOUT_MESSAGE+" "+callLength)
           .setCancelable(false)
           .setPositiveButton("Da", new DialogInterface.OnClickListener() {
               public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {


                        //DownloadTask d=new  DownloadTask(); 
                        //d.execute(Constants.WS_ADDRESS);   xmlPredlog.xml                                
               }
           })
           .setNegativeButton("Ne", new DialogInterface.OnClickListener() {
               public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {

                    dialog.cancel();
                    //showCustomToast("something");
                    //emptyList.setText("something");
               }
           });
    final AlertDialog alert = builder.create(); 
    alert.show();
  }

Everything compiles and runs, but when alert.show() is called, i get "AndroidRuntime(827): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application".

What is the solution here and for other cases, when you have to call some activity code from broadcast receivers?

DixieFlatline
  • 7,895
  • 24
  • 95
  • 147

1 Answers1

0

From the BroadcastReceiver doc

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

So either use the notification system or maybe start an activity that will look like a dialog

ccheneson
  • 49,072
  • 8
  • 63
  • 68