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??
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.
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.
A BroadcastReciever life cycle ends (ie stop receiving broadcast) when you unregister it.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With