如何设置定期AlarmManager每天执行code [英] How to Set Recurring AlarmManager to execute code daily

查看:270
本文介绍了如何设置定期AlarmManager每天执行code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写报警管理器,将在指定的时间期限内报警熄灭,每天。首先,我检查用户是否有该报警组的那一天:

  IF((User.getReminderTime(Home.this)大于0)
    &功放;&安培; (dt.getDate()!= today.getDate()|| dt.getDay()!=今天
      .getDay())){
   AppointmentManager.setFutureAppointmentCheck(这
     .getApplicationContext());
   User.setLongSetting(此,​​futureappts,today.getTime());
  }
 

然后,我去设置实际报警12和次日12:10之间熄灭:

 公共静态无效setFutureAppointmentCheck(上下文CON){
  AlarmManager AM =(AlarmManager)CON
    .getSystemService(Context.ALARM_SERVICE);

  日期futureDate =新的日期(新的日期()的getTime()+ 86400000);
  随机数发生器=新的随机();

  futureDate.setHours(0);
  futureDate.setMinutes(generator.nextInt(10));
  futureDate.setSeconds(0);

  意向意图=新的意图(CON,FutureAppointmentReciever.class);

  PendingIntent发送= PendingIntent.getBroadcast(CON,0,意图,
    PendingIntent.FLAG_ONE_SHOT);

  am.set(AlarmManager.RTC_WAKEUP,futureDate.getTime(),发送方);

 }
 

现在我设置一个测试环境,为这个熄灭每隔两分钟,似乎是工作的罚款,但是当我部署到实际设备中,reciever似乎并没有成为recieving报警。我想这可能是一个问题,该装置是睡着了,所以我增加了电源管理。但它仍然无法正常工作:

  PowerManager的PM =(电源管理器)上下文
    .getSystemService(Context.POWER_SERVICE);
  PowerManager.WakeLock WL = pm.newWakeLock(
    PowerManager.PARTIAL_WAKE_LOCK,KEEPALIVE);
  wl.acquire();
  setFutureAppointments(context.getApplicationContext());
  AppointmentManager.setFutureAppointmentCheck(上下文
    .getApplicationContext());
  User.setLongSetting(context.getApplicationContext(),futureappts,
    新的日期()的getTime())。
  wl.release();
 

任何人看到什么我做的公然错误的或者我要对这个错误?感谢您的任何和所有帮助。

解决方案

我通常做线沿线的更多的东西:

 意向书我=新的意图(这一点,MyService.class);
PendingIntent圆周率= PendingIntent.getService(此,0,I,0);
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(PI); //取消任何现有的警报
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime()+ AlarmManager.INTERVAL_DAY,
    AlarmManager.INTERVAL_DAY,PI);
 

这样,您就不必担心重新设置 AlarmManager 服务

我通常运行的code此位时,我的应用程序启动( onResume 在我的主要活动),并在的BroadcastReceiver 被设置为接收 BOOT_COMPLETED

我写了一个指导创建服务和使用 AlarmManager ,这是根据我自己的经验和一些技巧和放大器;招数我从看一个谷歌I / O谈话摘了下来。如果你有兴趣,你可以阅读这里


要在下面回答你的问题,我所能做的就是报价的文档

 公共无效setInexactRepeating(int型的,长triggerAtTime,间隔长,PendingIntent操作)
 

     

时间表,有不确切的触发时间要求重复报警;例如,重复每隔一小时,但是不一定在每一个小时的顶部报警。这些报警器能效更高比setRepeating提供严格的复发(INT,很长很长,PendingIntent),因为系统可以调节警报阶段,使它们同时开火,避免从睡眠状态唤醒设备​​超过必要的。

     

您的报警器的第一个触发器将不是要求的时间之前,但它可能不会发生,几乎在此之后一个完整的周期。此外,虽然重复报警的总体期间将被请求,报警的任何两个连续的点火之间的时间可以变化。如果你的应用需求非常低的抖动,使用setRepeating(INT,很长很长,PendingIntent)来代替。

总之,这不是很清楚。该文档只能说警报可能会有所不同。但是,它应该是重要的,你要知道,第一个触发的可能不是那个时候的。

后,出现了几乎整整间隔

I am currently trying to write alarm manager that will make an alarm go off within a specified period of time, daily. First I check to see if the user has had an alarm set for that for that day:

      if ((User.getReminderTime(Home.this) > 0)
    && (dt.getDate() != today.getDate() || dt.getDay() != today
      .getDay())) {
   AppointmentManager.setFutureAppointmentCheck(this
     .getApplicationContext());
   User.setLongSetting(this, "futureappts", today.getTime());
  }

Then I go and set the actual alarm to go off between 12 and 12:10 of the next day:

     public static void setFutureAppointmentCheck(Context con) {
  AlarmManager am = (AlarmManager) con
    .getSystemService(Context.ALARM_SERVICE);

  Date futureDate = new Date(new Date().getTime() + 86400000);
  Random generator = new Random();

  futureDate.setHours(0);
  futureDate.setMinutes(generator.nextInt(10));
  futureDate.setSeconds(0);

  Intent intent = new Intent(con, FutureAppointmentReciever.class);

  PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent,
    PendingIntent.FLAG_ONE_SHOT);

  am.set(AlarmManager.RTC_WAKEUP, futureDate.getTime(), sender);

 }

Now I setup a test environment for this to go off every two minutes and it seems to be working fine, however when I deploy to an actual device, the reciever does not seem to be recieving the alarms. I thought it might be an issue with the device being asleep, so I added the power manager. But it still does not work:

      PowerManager pm = (PowerManager) context
    .getSystemService(Context.POWER_SERVICE);
  PowerManager.WakeLock wl = pm.newWakeLock(
    PowerManager.PARTIAL_WAKE_LOCK, "keepAlive");
  wl.acquire();
  setFutureAppointments(context.getApplicationContext());
  AppointmentManager.setFutureAppointmentCheck(context
    .getApplicationContext());
  User.setLongSetting(context.getApplicationContext(), "futureappts",
    new Date().getTime());
  wl.release();

Anyone see anything I am doing blatantly wrong or am I going about this incorrectly? thanks for any and all help.

解决方案

I usually do something more along the lines of:

Intent i = new Intent(this, MyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi); // cancel any existing alarms
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
    AlarmManager.INTERVAL_DAY, pi);

This way, you don't have to worry about re-setting the AlarmManager in your Service.

I usually run this bit of code when my app starts (onResume in my main activity) and in a BroadcastReceiver that is set up to receive BOOT_COMPLETED.

I've written a guide on creating Services and using the AlarmManager, which is based on my own experience and a few tips & tricks I picked off from watching a Google I/O talk. If you're interested, you can read it here.


To answer your question below, all I can do is quote the docs:

public void setInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour. These alarms are more power-efficient than the strict recurrences supplied by setRepeating(int, long, long, PendingIntent), since the system can adjust alarms' phase to cause them to fire simultaneously, avoiding waking the device from sleep more than necessary.

Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time. In addition, while the overall period of the repeating alarm will be as requested, the time between any two successive firings of the alarm may vary. If your application demands very low jitter, use setRepeating(int, long, long, PendingIntent) instead.

In conclusion, it's not very clear. The docs only say that the alarm "may vary". However, it should be important for you to know that the first trigger might not occur for almost a full interval after that time.

这篇关于如何设置定期AlarmManager每天执行code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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