2

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.

ihtkwot
  • 1,252
  • 4
  • 16
  • 36

4 Answers4

3

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);   
    }
Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
  • so I would have to start a new activity that would handle the alert dialog? I think that is what you are saying. I edited my question to better reflect what I am trying to do. – ihtkwot May 30 '11 at 15:26
  • If its just a browser or not, you might want to just use an Intent (like the Google Talk client does for links): that I believe you can launch without an Activity. If you want to do more complex stuff then yes, you will need to launch an Activity to handle it. – Femi May 30 '11 at 15:44
  • It is just a browser but the idea is that the receiver checks incoming text messages, and if a keyword is found, say CNN, then it needs to pop open an alert dialog that asks the user if they want to go to the CNN.com website. – ihtkwot May 30 '11 at 17:30
  • I figured it out. I ended up using the intent method you suggested in your answer. Thanks. – ihtkwot May 30 '11 at 19:32
  • Hey @ihtkwot how did you do the same? can you please tell me? – Sandy Jun 22 '11 at 13:22
  • @Sandy, I followed the answer provided by Fermi. I'm not sure I understand your question. – ihtkwot Jun 24 '11 at 12:29
  • Work like charms !, I add your code to my SMSReceiver.java and I point out to my AlertDialogActivity.java (fill in the classname) and it works like charm !, thank you !!. – Bhimbim Aug 27 '13 at 10:05
1

If they allow it then any application can block the screen and can create problem to other applications. That's why I think, Android has not given us the leverage to use Alert window for broadcaster. I may be wrong but I tested it once and it didn't work that time.

Alternative, You can use TOAST

Sanjay Kumar
  • 1,474
  • 14
  • 22
0

if you want to open alert dialog after receiving response in onRecieve then add following code in OnRecieve, here i am alerting dialogue after clicking notification.

Intent intent = new Intent(mainContext,HomeActivity.class).putExtra("fromNotificationClick",true); 

PendingIntent pIntent = PendingIntent.getActivity(mainContext, (int) System.currentTimeMillis(), intent, 0);

then in your homeactivity add following code in onCreate,

boolean fromNotificationClick=false; 
Bundle extras=getIntent().getExtras();

 if(null!=extras) 
fromNotificationClick=extras.getBoolean("fromNotificationClick"); 

if(fromNotificationClick){ alert(); }

Finally add code for alert method.

public  void alert() {
        new AlertDialog.Builder(HomeActivity.this).setIcon(R.drawable.help).setTitle("Alert")
                .setMessage("Are you sure....")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getBaseContext(), "Coming to help", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getBaseContext(), "NO...", Toast.LENGTH_SHORT).show();
            }

        }).show();

    }
Ashpaq
  • 1
  • 1
0

You want the alert dialog to appear like it opens on top of everything. Use an intent to launch a new activity with a completely clear/see through background. This technique makes the alert dialog appear like its on top of the desktop with icons in the background ;-)

Noah Seidman
  • 4,359
  • 5
  • 26
  • 28
  • do you have any samples or resources for this? I've been looking for something like this to learn how to do it for a few days and still haven't found anything that actually works. – username Dec 14 '15 at 06:17