1

I have 2 alarms set, one for notifications, and the other one to do some tasks. My problem is that only one alarm seems to work( the notifications service one, the first alarm set). The Other alarm never goes off. Here is my code :

Intent myIntent1 = new Intent(getApplicationContext(), NotificationService.class);
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent1, 0);
        AlarmManager alarmManager1 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(System.currentTimeMillis());
        long frequency1 = 30 * 1000; // in ms
        alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), frequency1, pendingIntent);

        // Set alarm to fire go to Next day everyday at the same time
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
        calendar.set(Calendar.MINUTE, 57);
        calendar.setTimeInMillis(System.currentTimeMillis());
        Intent myintent = new Intent(getApplicationContext(), AlarmNextDayService.class);
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getService(getApplicationContext(), 11, myintent,0 );
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);

Any suggestions are welcome. I have looked atother sources as well nothing works for me till now. I have also added alarm permisison in the manifest file as the following :

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

Thank you

LoveMeow
  • 3,858
  • 9
  • 44
  • 66
  • 2
    Did you try adding a unique identifier on each intent like : `Intent intent = new Intent("uniqId", null, context, Receiver.class);` ? – Jscti May 31 '16 at 13:07
  • 1
    Do you have the second `Service` listed in the manifest? Also, do you realize that you're resetting the `Calendar` instance to the current time, after you set the hour and minute? Keep in mind, too, that an inexact alarm can be off by quite a bit for that interval. – Mike M. May 31 '16 at 13:13
  • 1
    turns out that was the problem, @MikeM. could you write it as an answer? – LoveMeow May 31 '16 at 16:06
  • 1
    It's cool. Simple fix. You could post the answer, if you like, or ask somebody who has already posted to amend theirs. Thank you, though. Glad you got it working. Cheers! (P.S. - You don't need the `SET_ALARM` permission for this.) – Mike M. May 31 '16 at 16:19

2 Answers2

1
Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
    calendar.set(Calendar.MINUTE, 57);
    calendar.setTimeInMillis(System.currentTimeMillis());

You are setting the HOUR_OF_DAY and the MINUTE but them you override that by calling setTimeInMillis(System.currentTimeMillis());

After that you set the alarm with the calendar.getTimeMillis() value which is already in the past, so the alarm is cancelled I think.

Tristan
  • 326
  • 2
  • 8
1

You're most likely seeing an issue because a Service is not guaranteed to run when triggered by an alarm, due to power save. If your device is on battery and idle when that alarm goes off, it will not trigger until the next time the device is full on or on AC power. You'll need to use a BroadcastReceiver which holds a wake lock which is then released by the Service when it is done. The WakefulBroadcastReceiver makes this a little easier to handle. This article will help provide more details.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33