title: AlertDialog from within BroadcastReceiver?? Can it be done?
body: <p>AlertDialog from within BroadcastReceiver? Can it be done?  I am working on a app that will pop up a Dialog box if I get SMS message.  I am trying to code this within a BroadcaseReceiver. But I cant use this line of code <code>AlertDialog.Builder builder = new AlertDialog.Builder(this);</code>.  Can someone please help me with a hint!</p>
  
  <pre><code>public class SMSPopUpReceiver extends BroadcastReceiver {
  
      private static final String LOG_TAG = "SMSReceiver";
      public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
      static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
  
      public void onReceive(Context context, Intent intent) {
          Log.i(LOG_TAG, "onReceive");
  
          if (intent.getAction().equals(SMSPopUpReceiver.ACTION)) {
              StringBuilder sb = new StringBuilder();
              Bundle bundle = intent.getExtras();
              if (bundle != null) {
              Object[] pdus = (Object[]) bundle.get("pdus");
  
              for (Object pdu : pdus){
                      SmsMessage messages =
              SmsMessage.createFromPdu((byte[]) pdu);
  
              sb.append("Received SMS\nFrom: ");
              sb.append(messages.getDisplayOriginatingAddress());
              sb.append("\n----Message----\n");
              sb.append( messages.getDisplayMessageBody());
              }
              }
              Log.i(SMSPopUpReceiver.LOG_TAG,
              "[SMSApp] onReceiveIntent: " + sb);
              Toast.makeText
              (context, sb.toString(), Toast.LENGTH_LONG).show();
              }
  
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("Are you sure you want to exit?")
                 .setCancelable(false)
                 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         dialog.cancel();
                     }
                 })
                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                          dialog.cancel();
                     }
                 });
          AlertDialog alert = builder.create();
      }
  
  }
  </code></pre>