Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to abort BroadcastReceiver in android

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

like image 658
Ahmad Abbasi Avatar asked Dec 26 '12 19:12

Ahmad Abbasi


People also ask

How do I stop BroadcastReceiver?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What is BroadcastReceiver Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is the life cycle of BroadcastReceiver in Android?

A BroadcastReciever life cycle ends (ie stop receiving broadcast) when you unregister it.

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.


2 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.

like image 90
CommonsWare Avatar answered Oct 04 '22 19:10

CommonsWare


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

like image 41
Ben Holland Avatar answered Oct 04 '22 17:10

Ben Holland