android – AlarmManager不会按时触发

android – AlarmManager不会按时触发,第1张

概述我希望AlarmManager每天上午10:30根据用户移动设备时间被解雇.它肯定会在上午10:30发射,但问题是在上午10:30之后,它会在没有时间的情况下重复,就像在每半小时之后或在任何不寻常的时间间隔之后. 如何防止这个问题?我在成功登录和注册ButtonCick()事件上调用它.如果用户注销,我也想停止此 *** 作. 我的代码如下: Intent myIntent = new Intent(Re 我希望AlarmManager每天上午10:30根据用户移动设备时间被解雇.它肯定会在上午10:30发射,但问题是在上午10:30之后,它会在没有时间的情况下重复,就像在每半小时之后或在任何不寻常的时间间隔之后.
如何防止这个问题?我在成功登录和注册buttonCick()事件上调用它.如果用户注销,我也想停止此 *** 作.
我的代码如下:

Intent myIntent = new Intent(Register.this,AlarmReceiver.class);        PendingIntent pendingIntent = PendingIntent.getbroadcast(Register.this,myIntent,0);        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);        Calendar firingCal = Calendar.getInstance();        Calendar currentCal = Calendar.getInstance();        firingCal.set(Calendar.HOUR_OF_DAY,10);        firingCal.set(Calendar.MINUTE,30);        firingCal.set(Calendar.SECOND,0);        long intendedTime = firingCal.getTimeInMillis();        long currentTime = currentCal.getTimeInMillis();        if (intendedTime >= currentTime) {            WakeLocker.acquire(getApplicationContext());            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,intendedTime,AlarmManager.INTERVAL_DAY,pendingIntent);            WakeLocker.release();        } else {            WakeLocker.acquire(getApplicationContext());            firingCal.add(Calendar.DAY_OF_MONTH,1);            intendedTime = firingCal.getTimeInMillis();            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,pendingIntent);            WakeLocker.release();        }
解决方法 代码似乎很好.如果目标版本是19,

注意:

as of API 19,all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms,rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlIEr than API 19 will continue to have all of their alarms,including repeating alarms,treated as exact.

资源:
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,android.app.PendingIntent)

注意:

Beginning in API 19,the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time,but may be deferred and delivered some time later. The OS will use this policy in order to “batch” alarms together across the entire system,minimizing the number of times the device needs to “wake up” and minimizing battery use. In general,alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

采用新的配料政策,交货订单保证不像以前那么强大.如果应用程序设置了多个警报,则这些警报的实际交付顺序可能与其请求的交付时间的顺序不匹配.如果您的应用程序具有强大的订购要求,则可以使用其他API来获取必要的行为;请参阅setwindow(int,PendingIntent)和setExact(int,PendingIntent).

targetSdkVersion在API 19之前的应用程序将继续获得先前的警报行为:所有计划的警报将被视为准确.

资源:
http://developer.android.com/reference/android/app/AlarmManager.html#set(int,android.app.PendingIntent)

请检查这是否适合您的要求.

总结

以上是内存溢出为你收集整理的android – AlarmManager不会按时触发全部内容,希望文章能够帮你解决android – AlarmManager不会按时触发所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127556.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存