Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is you are trying to show an <code>AlertDialog</code> from a <code>BroadcastReceiver</code>, which isn't allowed. You can't show an <code>AlertDialog</code> from a <code>BroadcastReceiver</code>. Only activities can display dialogs.</p> <p>You should do something else, have the <code>BroadcastReceiver</code> start on boot as you do and start an activity to show the dialog.</p> <p>Here is a <a href="http://commonsware.com/blog/2010/08/11/activity-notification-ordered-broadcast.html" rel="noreferrer">blog post</a> more on this.</p> <p>EDIT:</p> <p>Here is how I would recommend doing it. From your <code>BroadcastReceiver</code> start an <code>Activity</code> with an <code>AlertDialog</code> as such..</p> <pre><code>public class NotifySMSReceived extends Activity { private static final String LOG_TAG = "SMSReceiver"; public static final int NOTIFICATION_ID_RECEIVED = 0x1221; static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(ACTION); this.registerReceiver(mReceivedSMSReceiver, filter); } private void displayAlert() { 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(); alert.show(); } private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION.equals(action)) { //your SMS processing code displayAlert(); } } } } </code></pre> <p>As you see here I NEVER called <code>setContentView()</code>. This is because the activity will have a transparent view and only the alert dialog will show.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload