How can I set exact, repeating alarms in Android 4.4?

你。 提交于 2019-12-20 10:49:12

问题


Right now, I am setting alarms like this:

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        timedate = cal.getTime();

        //convert date to milis
        long timeInMilis = (timedate.getTime());

        //Set Alarm to Repeat
        manager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilis, interval, pendingIntent);

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

So, I would like to switch over to seting an exact repeating alarm. My device requires a minimum only of the latest API 19/Android 4.4, so I can't use setRepeating.

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


回答1:


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.



来源:https://stackoverflow.com/questions/26959511/how-can-i-set-exact-repeating-alarms-in-android-4-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!