I am starting a series of alarms inside an activity to start a service after specific time. After the service is started I want to cancel that particular alarm and not any other alarm.
Below is the code for starting the alarm.
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(StatAlarmActivity.this, AndroidService.class);
PendingIntent pIntent = PendingIntent.getService(StatAlarmActivity.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), i*24*60*60*1000, pIntent); //i varies, i is no of days after which the service will start
How can I cancel a particular alarm after the service is started? I tried below code inside the AndroidService but I can't access StartAlarmActivity.this and getSystemService here.
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(StatAlarmActivity.this, AndroidService.class);
PendingIntent pIntent = PendingIntent.getService(StatAlarmActivity.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pIntent);
Please help me out.
Try getActivity() instead of StatAlarmActivity.this if you are cancelling the alarm manager in a different activity than you start.
The first argument is the context.
From http://developer.android.com/reference/android/content/Intent.html#Intent(android.content.Context, java.lang.Class)
Syntax :
public Intent (Context packageContext, Class cls)
Parameters : packageContext A Context of the application package implementing this class. cls The component class that is to be used for the intent.
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