8

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??

Nikolai Samteladze
  • 7,337
  • 4
  • 41
  • 70
Ahmad Abbasi
  • 1,746
  • 5
  • 28
  • 43

4 Answers4

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.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    but what if i want to add multiple alarms?? – Dawood Ahmed Jan 07 '13 at 21:45
  • 4
    @DawoodAbbasi: Use distinct IDs for each (your `234324243`). – CommonsWare Jan 07 '13 at 21:50
  • 1
    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. – Dan Devine Jan 13 '13 at 07:12
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

Community
  • 1
  • 1
Ben Holland
  • 2,046
  • 4
  • 30
  • 49
  • i have to abort any pending intent not only current as i mentioned in question – Ahmad Abbasi Dec 27 '12 at 06:48
  • 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. – Ben Holland Dec 27 '12 at 19:22
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

Ramesh Sangili
  • 1,575
  • 3
  • 15
  • 30
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)

CodeShane
  • 6,350
  • 1
  • 16
  • 23