2

I have asked this before and God knows I have tried everything suggested. I am trying to have a service get executed once a day at a given time daily. I was able to set the alarm and broadcasts. But for some reason when the alarm gets fired, say 13h30, it's gonna get fired thereon at several irregular intervals. The only thing that's working properly is having the alarm fire at the correct time first, however it will then fire throughout the day and it's driving me crazy. Please help.

    PendingIntent reviewsPendingIntent = PendingIntent.getBroadcast(this,0,new Intent(this,ReviewReceiver.class),PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
    Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());

    Random random = new Random();
    int low = 7;
    int high = 22;
    int hour = random.nextInt(high - low) + low;
    int minute = random.nextInt(60 - 10) + 10;
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 13);
    cal.set(Calendar.MINUTE,15);
    cal.set(Calendar.SECOND,5);
    cal.set(Calendar.MILLISECOND,0);
    if(cal.getTimeInMillis() >= System.currentTimeMillis() ) {
        Log.i(TAG,"Set today");
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, reviewsPendingIntent);
    }else{
        Log.i(TAG,"Set tomorrow");
        cal.set(Calendar.DATE,1);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, reviewsPendingIntent);
    }

N.B. I plan to use the random integers to set my alarm when I get this working properly.

spongyboss
  • 8,016
  • 15
  • 48
  • 65
  • You want `cal.add(Calendar.DATE,1);`, not `cal.set(Calendar.DATE,1);`. You might also consider using `adb shell dumpsys alarm` to see if you have several alarms scheduled. – CommonsWare Feb 06 '16 at 12:54
  • everytime onCreate will be called your alarm code is called , if you are using service it will again be called mutliple time add `` refere this http://developer.android.com/intl/in/guide/topics/manifest/service-element.html – 1234567 Feb 06 '16 at 13:00
  • use an extra with intent, and start an alarm only of the extra is present with the intent check this http://stackoverflow.com/a/5265952/3126760 – 1234567 Feb 06 '16 at 13:04
  • I just did and went through the list of alarms, i found my app's alarm.... @abcd, I have made sure that the alarm it's not recreated every time onCreate is called.. i have set flags to prevent that – spongyboss Feb 06 '16 at 13:05
  • try this `manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);` for repeating after every 24 hours – 1234567 Feb 06 '16 at 13:08
  • @abcd isnt that equivelant to AlarmManager.INTERVAL_DAY? – spongyboss Feb 06 '16 at 13:12
  • 1
    Did you find a solution to your problem, because am also facing the same issue. – Bubunyo Nyavor Jul 03 '16 at 23:37

0 Answers0