I have an AlarmManager
that notifies me every 10 seconds. Everything works just fine but for some reason I can't cancel the alarm. Here's my code.
public class AlarmNotifReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
//things
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmNotifReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, pi); // Millisec * Second
}
public void CancelAlarm(Context context)
{
Intent i = new Intent(context, AlarmNotifReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
}
}
as expected I call this CancelAlarm()
method from my other classes but for some reason it does not cancel and keep notifying me like nothing happened.
Note: SetAlarm()
also works just fine.
Thanks in advance.