Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if alarm is set

I'm 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 i canceled alarmIsSet will continue to return true until I reboot the device. How do I fix this?

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;
    }
}
like image 925
Holm Avatar asked Jan 17 '11 13:01

Holm


People also ask

How do I know if alarm is set on iPhone?

To check for the alarm clock icon/indicator, swipe down from the top right of the screen to bring down your iPhone's control panel. If you have set an alarm, you'll see the clock icon just to the left of the battery indicator.

Why is my alarm not showing on screen?

You may have Sleep as Android enabled under Border lights (Edge Lightning on some versions). This causes the alarm popup to not display over the lockscreen, as the notification is being shown as a light around the edge of the screen. Disable Border lights for Sleep as Android in your device's settings.

How do I know if my alarm is set on iPhone 12?

Answer: A: The space in the top status bar is limited with the notch on the newer iPhones. To ensure that your alarm is on, you can swipe down from the upper right corner of the screen to bring up Control Center. If the alarm is on, the icon will appear near the battery and battery percent icon.


1 Answers

You just have to add

pi.cancel();

after

am.cancel(pi);
like image 189
lapis Avatar answered Sep 21 '22 14:09

lapis