2

Everyone is here saying use setExact for the API level 19 and higher but couldn't find how to set the repeating time in it so that it get repeated again at specified time.

So please tell me how to set the repeating time in setExact method?

here is my code

 Calendar calendar2,calendar,cal,cal2;
        calendar = new GregorianCalendar();
        calendar2 = new GregorianCalendar();

        calendar2.setTimeInMillis(System.currentTimeMillis());
        calendar.setTimeInMillis(System.currentTimeMillis());


        cal = new GregorianCalendar();
        cal2 = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
        cal.set(Calendar.HOUR_OF_DAY, 10);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
        cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
        cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));




        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent it = new Intent(MainActivity.this,Start_service_alarm.class);

        it.putExtra(Start_service_alarm.ACTION, Start_service_alarm.ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 987654321, it, 0);
        if(android.os.Build.VERSION.SDK_INT<19)
        {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }else
        {   
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

        }




        cal2.add(Calendar.DAY_OF_YEAR, calendar2.get(Calendar.DAY_OF_YEAR));
        cal2.set(Calendar.HOUR_OF_DAY, 20);
        cal2.set(Calendar.MINUTE, 0);
        cal2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND));
        cal2.set(Calendar.MILLISECOND, calendar2.get(Calendar.MILLISECOND));
        cal2.set(Calendar.DATE, calendar2.get(Calendar.DATE));
        cal2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH));


        AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent itIntent = new Intent(MainActivity.this,Stop_service_alarm.class);

        itIntent.putExtra("ACTION_STOP", Stop_service_alarm.ACTION_STOP);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 123456789, itIntent, 0);
        if(android.os.Build.VERSION.SDK_INT<19)
        {
        alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
        }else

        {
            alarmManager2.setExact(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pendingIntent2);
        }
Exception
  • 2,273
  • 1
  • 24
  • 42
Edward Collins
  • 353
  • 3
  • 13
  • Its as easy as calling this function again after processing. To set it for future. I have not delved deep into the Alarm manager above 19 API but, when I set an alarm for tomorrow - I use it to process something, once that process is complete I again can call the set alarm method to set it for tomorrow? – Skynet May 14 '15 at 08:47
  • @Skynet how can i do s o..? – Edward Collins May 14 '15 at 09:10
  • What do you call from your Alarm onReceive? - You must be performing some action like calling a method or starting a service. Now when your service finishes execution you can set the alarm again for tomorrow by calling Alarm.setAlarm? Can't you? – Skynet May 14 '15 at 09:12
  • @Skynet my service will be running between 10am to 8 pm after 8 how should i set the alarm for tomorrow – Edward Collins May 14 '15 at 09:15
  • How do you set the Alarm at the start of your application? On the first install? By calling a method, right? – Skynet May 14 '15 at 09:16
  • @Skynet as i have posted the code i am setting like this on start of my application – Edward Collins May 14 '15 at 09:24
  • Do the same at the end of your service. `BY CALLING THE ABOVE METHOD AGAIN`. – Skynet May 14 '15 at 09:42
  • @Skynet but its flickering like calling both alarm manger at same time. after 8 – Edward Collins May 14 '15 at 09:44
  • What is flickering? Use [Traceview](http://developer.android.com/tools/debugging/debugging-tracing.html) to clean junk from your program! – Skynet May 14 '15 at 09:55
  • @Skynet not flickering but its calling both alarm manger at same time after calling it again when service stops working – Edward Collins May 14 '15 at 10:04
  • I am sure you can figure that out yourself, of how to call them one after the other. – Skynet May 14 '15 at 10:27
  • @Skynet no i can't i tried my level best but didn't find any solution that's why asked question. if you know exactly how to do this than let me know else do not waste my time neither yours – Edward Collins May 14 '15 at 10:33
  • Facing same problem. Have got the solution? @Edward Collins – Sid Feb 11 '16 at 07:05

1 Answers1

-2

Unfortunately, the scheduled times for repeating are inexact and I read that they could be inexact for a full interval!

If your targetSdkVersion is 19 or higher, yes.

What can I do instead to set an exact repeating alarm?

Use setExact() to get control for your initial delay. Then, as part of your work for processing that event, use setExact() to get control at your next desired time. IOW, you do the "repeating" part yourself.

Yes, this is irritating.

It is intentionally irritating, to steer developers to not use exact repeating alarms, as those are worse for the battery than are their inexact brethren. If the user will perceive the inexact behavior and will not appreciate it, feel free to use setExact() to make the alarms occur when the user wants. However, if the user will not perceive the inexact behavior, please use inexact alarms, as the user may notice the battery impact of your exact alarms.

Rajan Bhavsar
  • 1,977
  • 11
  • 25
  • Seem you have copied the saner from commons ware, if u have something solid with you or exactly what should i do or should i do the repeating part than tell me – Edward Collins May 14 '15 at 09:11
  • Hi Edward As per i understood the Scenario you gave in Question and i got up to result that : Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested. – Rajan Bhavsar May 14 '15 at 09:16
  • Me have read all document i have posed my question to get the code of how to do that instead of any documentation on that – Edward Collins May 14 '15 at 10:05