4

I know this type of questions are aksed so many times...but please first read my question before down voting or marking as duplicate.

I have referred so many SO questions like this for managing alarm but cant find any solution so i am here.

In my app i want to trigger repeating alarm one at daily 8.00 am and others as user specified.

First i tired to repeat alarm using setRepeating...this is my code

Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.AM);
            //calendar.set(Calendar.MILLISECOND, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent pintent = new Intent(this, AlarmReceiver.class);

            pintent.putExtra("id", 0);
            pintent.putExtra("ontime", false);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    pintent, PendingIntent.FLAG_UPDATE_CURRENT);


            am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

Actually the problem is alarm is not firing after long sleep. If it test using changing date and time it fires perfactly....but it stops firing after a night...

So i switched to second option as mentioned in given link. I set alarm using setExact as follow and reschedule it on each fire.

 Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.AM);
            //calendar.set(Calendar.MILLISECOND, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent pintent = new Intent(this, AlarmReceiver.class);

            pintent.putExtra("id", 0);
            pintent.putExtra("ontime", false);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    pintent, PendingIntent.FLAG_UPDATE_CURRENT);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent);
                am.setAlarmClock(alarmClockInfo, pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

But steel i am having same issue. It is not repeating daily. Please also consider this is only the example of my default alarm. There can be N no of exact and repeating alarms as user specified.

Someone also suggest me to use service rather than alarm manager, but my service might be killed so i am afraid to use that. One other solution is to check due date and set all alarm daily, but i dont think this is the right way. Is it?

Please suggest me any other method or help me to improve my code. I badly need this to work. Thanx all. My testing devices are HTC One X (4.3), Redmi Prime(6.0) and Mi4i (5.0.2)

Community
  • 1
  • 1
H Raval
  • 1,903
  • 17
  • 39

1 Answers1

0

I had many problems setting repeating alarms, but in the end this code was working on all my testing devices, maybe this helps:

    // set time
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR_OF_DAY, 8);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);

    long startUpTime = c.getTimeInMillis();

    // startupTime + 24 hours if alarm is in past
    if (System.currentTimeMillis() > startUpTime) {
        startUpTime = startUpTime + 24 * 60 * 60 * 1000;
    }

    // initialize alarm
    AlarmIntent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);
Tyrmos
  • 387
  • 4
  • 13
  • 1
    ok...so INTERVAL_DAY is problematic right? but what if i want to repeat alarm weekly or monthly – H Raval Oct 26 '16 at 08:26
  • INTERVAL_DAY isnt the only difference between my and your code, you could at example try to use .setInexactRepeating instead of .setRepeating. If you want to do it weekly you could just multiply the interval with 7 (24 * 60 * 60 * 1000 = 1 day, 7 * 24 * 60 * 60 * 1000 = 1 week) – Tyrmos Oct 26 '16 at 08:33
  • but i want my alarm to fire at exact time so i cant use setInexactRepeating – H Raval Oct 26 '16 at 08:35
  • Do you really need the alarm to fire at exact time to the second? Because if not, setInexactRepeating is better. Since KitKat, setRepeating and setInexactRepeating are the same, see [here](https://developer.android.com/reference/android/app/AlarmManager.html). – Tyrmos Oct 26 '16 at 08:44
  • well its ok if it just delay in seconds...but i have tested it and it delays in minutes...so its not feasible for me – H Raval Oct 26 '16 at 08:49
  • Below API 19, you can use setExactRepeating, but above API 19, you could use setExact(type, triggerAt, PendingIntent), but youve to manage the repeating yourself. You can achieve this by calling setExact inside your onReceive() in your AlarmReceiver class. But be careful using setExact, as the battery drain is much higher. – Tyrmos Oct 26 '16 at 09:16
  • thanx for your support but i have already mentioned in my question that i using this scenario but it is also not working...after 2-3 days alarm stop firing...i dont know why – H Raval Oct 26 '16 at 09:24
  • 1
    Once you set the alarm It should work and repeating continuously until restart the device. once restarted the device alarm manager losses all the alarms, so we should set the alarm again when system booted (boot_completed_receiver). – Kona Suresh Oct 26 '16 at 09:45