Search code examples
androidalarmmanagerandroid-alarmssetalarmclock

How to avoid SystemAlarms creeping to my application getNextAlarmClock() of my application


I am building an application where I set Alarms for particular times.For this, I used AlarmClock of Android's to set Alarms.Here I try showing next AlarmClock time on his application to know the very next alarm.

Eg: I set 9:00AM, 6:00PM alarms using alarmManager.setAlarmClock(...) ,My Application shows next Alarm time using alarmManager.getNextAlarmClock() on homescreen saying 9:00AM.

Here is an issue I am facing where when I set some other alarms on my System provided Clocks App and set Alarm for 8:00 AM.This alarm time is getting reflected in my application next alarm time showing that the next time is set for 8:00 Am instead of 9:00 AM., which it is not supposed to.

 private String getNextAlarmClock() {

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        AlarmManager.AlarmClockInfo alarmClockInfo = alarmManager.getNextAlarmClock();

        Log.d(TAG, "time of next alarm in millis" + alarmClockInfo.getTriggerTime());

        return alarmClockInfo.getTriggerTime() + "";

}

could anyone please help me in this regard. This help is Appreciated, please.

Thanks in Advance!


Solution

  • If you call System Alarm manager, then this is common to all the applications in your mobile. And getNextAlarmClock() will give you the next alarm set in your mobile and not by your application. If you want alarm specific to your application, then you have to create this without using Alarm Manager.

    If you are ok to get alarmed by system alarm manager and issue is only with display of next alarm time, then i would recommend to use system alarm manager to set time and save this information to DB/shared preference. To display next alarm time, you can fetch saved data from DB instead of taking from alarm manager.