I am trying to stop the alarm and checking it whether it stopped or not, but it always returns true means alarm is working. I tried to stop the alarm based on the answer in the link https://stackoverflow.com/a/17616299/1226882 but it doesnt work for me.
Please refer the below code
Start Alarm
public static void startSchedulerAlaram(Context ctx) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (calendar.compareTo(Calendar.getInstance()) <= 0) calendar.add(Calendar.DATE, 1); AlarmManager alarmManager = (AlarmManager) ctx .getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(ctx, Alaram_Receiver.class); intent.setAction(Utility.SCHEDULE_ACTION); PendingIntent pi = PendingIntent.getBroadcast(ctx, 1, intent,0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi); log("Scheduler Alarm", "Started"); }
Stop Alarm
public static void stopSchedulerAlaram(Context ctx) { Intent intent = new Intent(ctx, Alaram_Receiver.class); intent.setAction(Utility.SCHEDULE_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) ctx .getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); log("Scheduler Alarm", "Stopped"); }
Check Alarm
public static boolean checkSchedulerAlaram(Context ctx) { boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION), PendingIntent.FLAG_NO_CREATE) != null); return alarmUp; }