How to cancel BroadcastReceiver in android

I am making an SMS schedule application that will simply take time, sms and number from the user and send this sms at the set time. I am using PendingIntent . Here is my sample code.

When the user creates a schedule, he 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 file 'SMSBroadcastReceiver.java'

 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 a user edits a schedule, how can I cancel this broadcast and send a new one? Since there can be more than one schedule, how to find speicific for change / interrupt?

+8
android android-pendingintent broadcastreceiver smsmanager
source share
4 answers

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

Call cancel() on AlarmManager with the equivalent of PendingIntent one you used to configure 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); 

How can there be more than one graph, how to find speicific for change / interrupt?

There is only one signal in your implementation.

+10
source share

If you have a link to a pending intention, you can cancel the intention. If you want to interrupt the current broadcast, you can use abortBroadcast .

See also:

1) How to get and cancel PendingIntent?

2) Stop pending intentions

3) Cancel / Cancel broadcasts

+6
source share
  • public final void abortBroadcast ()

Added to API Level 1 Sets a flag indicating that this receiver should interrupt the current transmission; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent reception of other broadcast receivers. It will still call onReceive (Context, Intention) the BroadcastReceiver passed to the calling Context.sendOrderedBroadcast.

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

0
source share

If you just change Timemilli , you can call your sendMessage() function sendMessage() .

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

The AlarmManager.set () function will automatically cancel the previous alarm until the pendingIntent parameter changes; it does not have to be the same instance, but it needs a β€œmatch” according to Intent.filterEquals ():

Because of this behavior, it is important to know when two intentions are considered the same for the purpose of receiving a PendingIntent. A common mistake people make is to create several PendingIntent objects with Intents that change only in their "superfluous" content, expecting to receive a different PendingIntent each time. This is not happening. The parts of the intent that are used for matching are the same as those defined by Intent.filterEquals. If you use two Intent objects that are equivalent to 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)

0
source share

All Articles