ihtkwot ihtkwot - 1 year ago 80
Android Question

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

So I have a Broadcast Receiver that listens for incoming SMS and searches the SMS for particular terms. I implemented the receiver in the manifest and in a separate class from my activity.

*EDIT:*
Depending on whether or not the SMS contains a particular term I need a dialog box to pop up and in that dialog box the user will be presented with an option to go to a particular website or not go to a particular website.

If I was doing this in another language I would just have a global variable that the receiver modify and then the other method would be checking it's value to determine whether or not it needed to execute something, but I can't do that in android.

Answer Source

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);   
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download