Home » Android » How to abort BroadcastReceiver in android

How to abort BroadcastReceiver in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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.

Answer:

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

Answer:

  • 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

Answer:

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)