I've found many questions on SO regarding this topic but none could help me, my alarm just doesn't cancel this is my code
set alarm in somclass
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_yes )
{
Long time = MainActivity.targetTime.getTimeInMillis();
int requestID = IDGenerator.incrementalRequestID(context);
Intent intentAlarm = new Intent(context, SomeTask.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Log.v("conclick", requestID+"");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestID,
intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, time , pendingIntent );
}
dismiss();
}
cancel it in another
public static void cancelAlarm( Context context , int requestID)
{
Log.v("cancelling", requestID+"");
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
}
this may be problem replace this
Intent intent = new Intent();
to
Intent intentAlarm = new Intent(context, SomeTask.class);
it should be like this
public static void cancelAlarm( Context context , int requestID)
{
Log.v("cancelling", requestID+"");
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentAlarm = new Intent(context, SomeTask.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
requestID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
}