问题描述
阅读所有质量检查后,我没有得到任何适当的解决方案.我有2个问题
1.即使我仅在清单中注册接收器,警报也会触发两次.(不是通过代码)
2.当我更新警报间隔时间时,它会随机触发
after reading all the QA i didnt get any proper solution.I have 2 problems
1. Alarm fires twice even if i register my receiver only in manifest.(not by code)
2. when i update interval time of alarm it gets fires randomly
这是我设置闹钟的方法
public void AlarmCall(int min) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pintent = PendingIntent.getBroadcast(context,0 , intent, 0);
alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
cancelAlarm(alarm,pintent);
if(Build.VERSION.SDK_INT<18) {
alarm.set(AlarmManager.RTC_WAKEUP, 1000 * 60 * min, pintent);
}
else if(Build.VERSION.SDK_INT>=19 && Build.VERSION.SDK_INT<=22)
{ alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 1000*60*min, pintent);
}
else if(Build.VERSION.SDK_INT>=23)
{ alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,1000*60*min,pintent);
}
}
取消警报的方法:
public void cancelAlarm(AlarmManager alarm,PendingIntent p)
{
alarm.cancel(p);
Log.d("Alarm","Alarm Cancle");
}
在我的项目应用程序类中,我必须以10分钟的时间间隔启动警报,并且它可以正常工作,根据用户输入的值,我需要更新时间间隔.因此,我使用int min输入值调用此方法并取消了第一个警报.
但是在棉花糖中,它每5秒钟触发一次,而奇巧棒棒糖则随机地触发.
甚至使用setExact()方法进行检查
in my project Application class i have to start alarm with 10 min time interval and it works fine , according to user input value i need to update time interval.
so i call this method with int min input value and cancel first alarm.
but in marshmallow it fires at every 5 seconds, and kitkat lollipop it fires randmoly.
even checked with setExact() method
推荐答案
我遇到了同样的问题,使用setWindow解决了问题
I had the same issue, use setWindow solved the problem
long repeatInterval = 1000 * 60 * min;
long triggerTime = SystemClock.elapsedRealtime()
+ repeatInterval;
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 19)
{
alarms.setWindow(AlarmManager.RTC_WAKEUP,
triggerTime,
repeatInterval,
pendingIntent);
}else{
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
triggerTime,
repeatInterval,
pendingIntent);}
这篇关于更新间隔时间时警报管理器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!