Show an Alert Dialog in Broadcast Receiver After a System Reboot

show an alert dialog in broadcast receiver after a system reboot

The problem is you are trying to show an AlertDialog from a BroadcastReceiver, which isn't allowed. You can't show an AlertDialog from a BroadcastReceiver. Only activities can display dialogs.

You should do something else, have the BroadcastReceiver start on boot as you do and start an activity to show the dialog.

Here is a blog post more on this.

EDIT:

Here is how I would recommend doing it. From your BroadcastReceiver start an Activity with an AlertDialog as such..

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();
}
}
}
}

As you see here I NEVER called setContentView(). This is because the activity will have a transparent view and only the alert dialog will show.

BroadcastReceiver onReceive open dialog

If you want to show a dialog from inside your onReceive of the BroadcastReceiver, inside your broadcast receiver you may start a transparent activity with an alert dialog and NEVER called setContentView(). The activity will have an transparent view and only the alert dialog will show.
Source: show an alert dialog in broadcast receiver after a system reboot

There are many similar posts which talk about this topic. See below questions for code samples and other reviews on the same:


  1. AlertDialog from within BroadcastReceiver?? Can it be done?

  2. How to raise an alert dialog from BroadcastReceiver class?

  3. How can I display a dialog from an Android broadcast receiver?

  4. How to setup Alertbox from BroadcastReceiver

Hope this will help.

Show dialog in broadcast receiver

You should declare all your activities in AndroidManifest.xml before using them as Intent. Place the activity BatteryDialog in the Android manifest properly.

Try something like this:

<activity android:name= ".BatteryDialog" />

Android Broadcastreceiver why popup is launching

In my opinion, your 'showDialog()' method is called by using Context from an Activity (your MainActivity) which is allowed, not by using Context from a BroadcastReceiver which is not allowed.
You can refer this answer for more details: show an alert dialog in broadcast receiver after a system reboot

How to setup Alertbox from BroadcastReceiver



Although you can not show AlertDialog from Receivers because it needs ActivityContext.

You have an alternate solution to show an Activity like AlertDialog from Receiver. This is possible.

To start Activity as dialog you should set theme of activity in manifest as <activity android:theme="@android:style/Theme.Dialog" />

Style Any Activity as an Alert Dialog in Android


To start Activity from Receiver use code like

    //Intent mIntent = new Intent();
//mIntent.setClassName("com.test", "com.test.YourActivity");
Intent mIntent = new Intent(context,YourActivity.class) //Same as above two lines
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);

And one more reason behind not using AlertDialog from receiver (Even if you managed to show AlertDialog) is

A BroadcastReceiver object is only valid for the duration of the call
to onReceive(Context, Intent). Once your code returns from this
function, the system considers the object to be finished and no longer
active.

This has important repercussions to what you can do in an
onReceive(Context, Intent) implementation: anything that requires
asynchronous operation is not available, because you will need to
return from the function to handle the asynchronous operation, but at
that point the BroadcastReceiver is no longer active and thus the
system is free to kill its process before the asynchronous operation
completes.

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. More...

So the better way is 'show notification' and alternate way is 'to use Activity as an Alert..'

Happy coding :)

Show progressDialog on fragment BroadcastReceiver

You can't set contentView before showing progress dialog. See this link

You can find more info here.

How do you use an alert dialog box in a broadcast receiver in android?

I'm not sure your title and your question match up, but if what you want is to open up a website in the browser just fire off an Intent from within your BroadcastReceiver.

If you want an explicit AlertDialog then see AlertDialog from within BroadcastReceiver?? Can it be done? for some detail: you will have to use an Intent and start an Activity with the new task flag set.

EDIT: you'd do something like this:

public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, {CLASSNAME}.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}


Related Topics



Leave a reply



Submit