重复报警管理在API精确间隔=> 19?间隔、精确、API、GT

2023-09-06 07:25:09 作者:感情都泛滥

我有一个庞大的阅读,我仍然认为这是没有一个明确的/完整的回答了这个问题。

I had a massive reading and still I think there is not a clear/complete Answer to this question.

一是一些东西澄清:这个问题是不是与省电关注的手机,但更多的precise时间,我在Android的一个新手。

First some stuff to clarify : this question is not concern with battery saving on phone but more about precise timing and I Am a newbie in Android.

现在让我更深入地解释这个问题。我对给定的间隔报警管理器,将调用敬酒(为简单起见)(每2分钟) manager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的(),间隔的PendingIntent);以上将调用一个BroadcastReciver一个的onReceive()梅索德。

Now let me explain the question in more depth. I have a Alarm manager that will invoke a toast (for simplicity) on given interval (every 2 minutes) manager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), interval, pendingIntent); The above will invoke a onReceive() methode on a BroadcastReciver.

public class AlarmReceiver extends BroadcastReceiver  {
@Override
public void onReceive(Context context, Intent intent) {
    //Toast ....bala blah
   }}

现在这是在API&LT确切的区间;但19 API中=> 19 setRepeating()不准确了!我发现一些人建议(在其他形式)使用setExact()。但对如何使用setExact()在区间(或我无法找到)没有例子或明确的解释。据我了解setExact()是不同于setRepeating开关动作(),所以根据一些人会在下一个时间表需要通过的onReceive()来设置(再次我无法找到一个例子。(反正这是我现在我在哪里。我真的鸭preciate任何意见或建议,或者链接或例子...

Now This was exact interval on API<19 however In API=>19 setRepeating() is not exact anymore ! I found some people suggested (on the other forms) to use setExact(). but there is no example or clear explanation on how to use setExact() in Interval (or I could not find that). As far as I understood setExact() is on off action unlike setRepeating() so according to some others next schedule needs to be set through onReceive() (again I could not find an example ;( . Anyhow this is where I am now . And I really Appreciate any comment or suggestion or link or example ...

我希望,我问我的问题不够清晰,顺便说一下,如果有另一种方法来这个(关于在API precise间隔运行的任务> 19,请让我太了解)非常感谢

I hope I asked my question clear enough, by the way if there is another approach to this (for running a task on precise intervals in API>19 please let me know too) Many thanks

推荐答案

您可以使用 setExact 类似于 setRepeating

void scheduleAlarm(Context context) {
    AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent yourIntent = new Intent();
    //TODO configure your intent
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, MyApplication.ALARM_REQUEST_CODE, yourIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeToWakeUp, alarmIntent);
}

这两个区别是:

的时间是准确的(或尽可能接近)

您将有计划在的onReceive 您的BroadcastReceiver的下一次出现。 The timing will be exact (or as nearly as possible)

You will have to schedule the next occurrence in the onReceive of your BroadcastReceiver.

  public class AlarmReceiver extends BroadcastReceiver  {
  @Override
  public void onReceive(Context context, Intent intent) {
    //TODO process alarm
    scheduleAlarm(context);
  }}