Reputation: 5440
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.
Upvotes: 3
Views: 115
Reputation: 1780
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.
Upvotes: 1