在没有立即触发的Andr​​oid重复报警经理 [英] Android repeat alarm manager in not triggering immediately

查看:184
本文介绍了在没有立即触发的Andr​​oid重复报警经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code:

 日历calSet = Calendar.getInstance();
calSet.set(Calendar.HOUR_OF_DAY,11);
calSet.set(Calendar.MINUTE,20);
calSet.set(Calendar.SECOND,0);
calSet.set(Calendar.MILLISECOND,0);

PendingIntent PI = PendingIntent.getBroadcast(背景下,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),PI);
 

和说,我执行的11:30。报警立即触发(预计)。 但是,对于同样的,当我使用

<$p$p><$c$c>alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),24*60*60*1000,pi);

报警没有立即触发。有延迟长达1分钟(这是不期望的)。 我想重复报警,立即触发没有任何延迟。

可能有人请帮助我?

解决方案

您应该改变你的code到:

  PendingIntent PI = PendingIntent.getBroadcast(背景下,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
如果(android.os.Build.VERSION.SDK_INT&GT; = android.os.Build.VERSION_ codeS.KITKAT){
      alarmManager.setExact(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),PI);
} 其他 {
      alarmManager.set(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),PI);
}
 

@Edit 以上code将努力为准确设置时间。但本节将介绍有关重复报警经理。

对于低于19 API,我们使用 AlarmManager.setRepeating()将报警定时触发正好在指定的时间。但是,从19和更新,这种方法不会再工作,没有任何API的支持这种行为。我觉得这个API的变化使开发商想更仔细地时,他们创建一个计时器。由于定时器触发在完全相同的时间定期将耗尽的电池这么多。

如果你愿意,你必须做你自己。首先,将 AlarmManager.setExact(),当报警触发,你会在下一次做出报警触发再次手动

下面是code:

  PendingIntent PI = PendingIntent.getBroadcast(背景下,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
如果(android.os.Build.VERSION.SDK_INT&GT; = android.os.Build.VERSION_ codeS.KITKAT){
     alarmManager。 setExact(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),PI);
} 其他 {
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),PI);
}
 

而在你的意图,你把手柄code,你应该检查是否Android的API> = 19,应用程序将创造下一个事件的新报警。

 如果(android.os.Build.VERSION.SDK_INT&GT; = android.os.Build.VERSION_ codeS.KITKAT){
         做一点事();
         //计算时间为下一个事件
         日历nextEvent = calcNextEvent();
         再次//并设置报警
         alarmManager。 setExact(AlarmManager.RTC_WAKEUP,nextEvent.getTimeInMillis(),PI);
 } 其他 {
         做一点事();
 }
 

我认为这是Android的API设计问题。老code应该在较新的版本。无论如何,这个新的API设计让一切更清晰的开发商,系统(节省电池)更好。当然,当你使用新的API:)

希望这有助于:)

My code:

Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.HOUR_OF_DAY, 11);
calSet.set(Calendar.MINUTE, 20);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

PendingIntent pi=PendingIntent.getBroadcast(context,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),pi);

and say, I'm executing at 11:30. Alarm triggers immediately (which is expected). But, for the same when I use

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calSet.getTimeInMillis(),24*60*60*1000,pi);

alarm is not triggered immediately. There is a delay up to 1 minute (which is not expected). I want repeating alarm to trigger immediately without any delay.

Could someone please help me with this?

解决方案

You should change your code to :

PendingIntent pi=PendingIntent.getBroadcast(context,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi);
} else {
      alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi);
}

@Edit Above code will work for set exactly time. But this section will explain about repeating for alarm manager.

For api below 19, we use AlarmManager.setRepeating() will make alarms trigger exactly at specified time periodically. But from 19 and newer, this method won't work again and there aren't any apis support this behavior. I think this api change make developers thinking more carefully when they create a timer. Because a timer trigger at exactly time periodically will drain battery so much.

If you want you must do on your own. Firstly, you set AlarmManager.setExact() and when alarm trigger, you will make alarm trigger again next time manually

Here is the code:

PendingIntent pi=PendingIntent.getBroadcast(context,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
     alarmManager. setExact(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi);
} else {
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pi);
}

And in your intent, where you put handle code, you should check if android api >= 19, application will create new alarm for the next event.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
         doSomething();
         // calculate time for next event
         Calendar nextEvent = calcNextEvent();
         // and set alarm again
         alarmManager. setExact(AlarmManager.RTC_WAKEUP, nextEvent.getTimeInMillis(), pi);
 } else {
         doSomething();
 }

I think this is problem in android api design. Old code should work on newer version. Anyway, this new api design make everything clearer for developer, better for system(save battery). Of course, when you use new api :)

Hope this help :)

这篇关于在没有立即触发的Andr​​oid重复报警经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆