How to abort BroadcastReceiver in android
Asked Answered
A

4

9

I am making an SMS schedule app which will simply take time, sms and number from user and send this sms at a given time. I am using PendingIntent. Here is my sample code.

When user creates a schedule, it simply calls this method.

private void SendMessages()
    {
        Intent intent = new Intent(this, SMSBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, Timemilli, pendingIntent);
    }

And here is the 'SMSBroadcastReceiver.java' file

public class SMSBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "SMS Sent !!!!.", Toast.LENGTH_LONG)
                .show();
        // //Sms Sending

        try
        {
            SmsManager smsManager = SmsManager.getDefault();
            for (int i = 0; i < SMSScheduleContactsActivity.SelectedContacts.length; i++)
            {
                smsManager.sendTextMessage(
                        SMSScheduleContactsActivity.SelectedContacts[i], null,
                        AddNewSmsSchedule.Message, null, null);
            }

            Log.d("testllllllllllllllll", "Message Sent");
        }
        catch (Exception e)
        {   
            e.printStackTrace();
        }
    }

}

My question is: when user edits the schedule, how can I cancel that broadcast and send a new one??? As there can be more than one schedule, how to find the speicific to change/abort??

Acute answered 26/12, 2012 at 19:0 Comment(0)
L
10

when user edits the schedule, how can I cancel that broadcast and send a new one?

Call cancel() on AlarmManager with an equivalent PendingIntent to the one you used to set up the schedule:

Intent intent = new Intent(this, SMSBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(pendingIntent);

As there can be more than one schedule, how to find the speicific to change/abort?

There is only one alarm in your implementation.

Lemmon answered 7/1, 2013 at 15:43 Comment(3)
but what if i want to add multiple alarms??Newlywed
@DawoodAbbasi: Use distinct IDs for each (your 234324243).Lemmon
It's easy to structure it so that only one alarm is active at any time.Try sorting all of your pending alarms based on the desired delivery time, then just schedule the nearest one. When that alarm fires, look at your list (keep in database?) and pick the nearest one again.Schism
M
6

If you have a reference to the pending Intent then you can cancel the Intent. If you want to abort a current broadcast you can use abortBroadcast.

See also:

1) How to get and cancel a PendingIntent?

2) Stop pending intent

3) Aborting/Cancelling Broadcasts

Mraz answered 26/12, 2012 at 19:12 Comment(2)
i have to abort any pending intent not only current as i mentioned in questionAcute
Perhaps you should keep a reference to each new pending intent you make, then if you find you need to cancel it later, you can iterate over your pending intents and use the cancel method mentioned above.Mraz
S
0
  • public final void abortBroadcast ()

Added in API level 1 Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent any other broadcast receivers from receiving the broadcast. It will still call onReceive(Context, Intent) of the BroadcastReceiver that the caller of Context.sendOrderedBroadcast passed in.

This method does not work with non-ordered broadcasts such as those sent with Context.sendBroadcast

Saltant answered 9/1, 2013 at 21:44 Comment(0)
G
0

If you are just changing Timemilli, you can call your sendMessage() function again.

alarmManager.set(AlarmManager.RTC_WAKEUP, Timemilli, pendingIntent);

The AlarmManager.set() function will cancel the previous alarm automatically as long as the pendingIntent is the same; it does not have to be the same instance, but needs to 'match' according to Intent.filterEquals():

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

http://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent)

http://developer.android.com/reference/android/app/PendingIntent.html http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)

Gamboge answered 10/1, 2013 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.