How to check if an alarm is set

I am trying to check if my alarm is active or not. The alarmIsSet method will return false before the alarm is set, true when the alarm is set. So far so good, however, after the alarm that I canceled, alarmIsSet will continue to return true until I reboot the device. How to fix it?

public class Alarm extends Activity { private Intent intent = new Intent("PROPOSE_A_TOAST"); private void alarm (boolean activate) { AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); if(activate == true) { int type = AlarmManager.ELAPSED_REALTIME_WAKEUP; long interval = 3000; long triggerTime = SystemClock.elapsedRealtime(); am.setRepeating(type, triggerTime, interval, pi); } else { am.cancel(pi); } } private boolean alarmIsSet() { return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE) != null; } } 
+8
android alarmmanager alarm
source share
3 answers

You just need to add

 pi.cancel(); 

after

 am.cancel(pi); 
+14
source share

After some headaches with this material, I found out that if I somehow created pending intentions when testing the material, it would not actually clear between tests. Even killing the application did not. The intention still remained in the system and continued to return to the truth upon verification. I actually had to write code to kill it before it was properly tested.

+1
source share

The easiest way is to check the values ​​(dates and) in the alarm variable, if it is not the same value as in the absence of an alarm (so you can check what it is), then it will indicate that the alarm is active, and during Checks in the program are either the elapsed time and the sound signal, or the time that has not yet arrived, and the signal has not yet gone out or not gone out. Please note that the rules can only allow one alarm activation per device session before rebooting or turning off the power, or every 12 or 24 hours, and this may be the reason that the status is not cleared.

0
source share

Source: https://habr.com/ru/post/651164/


All Articles