【问题标题】:Repeat Alarm Manager At Exact Interval in API=>19?在 API => 19 中以精确间隔重复警报管理器?
【发布时间】:2015-03-31 22:15:43
【问题描述】:

我阅读了大量资料,但我仍然认为这个问题没有明确/完整的答案。

首先要澄清一些事情: 这个问题与手机的电池节省无关,而是与精确计时有关,我是 Android 的新手。

现在让我更深入地解释这个问题。我有一个警报管理器,它将在给定的时间间隔(每 2 分钟)调用一个 toast(为简单起见)manager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), interval, pendingIntent); 以上将在 BroadcastReciver 上调用 onReceive() 方法。

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

现在这是 API19 中,setRepeating() 不再精确了!我发现有些人建议(在其他表单上)使用 setExact()。但是没有关于如何在 Interval 中使用 setExact() 的示例或明确的解释(或者我找不到)。据我了解,setExact() 与 setRepeating() 不同,因此根据其他一些人的说法,下一个时间表需要通过 onReceive() 设置(我再次找不到示例;(。无论如何这就是我现在的位置。我真的很感激任何评论或建议或链接或示例...

我希望我的问题问得足够清楚,顺便说一句,如果有其他方法可以解决这个问题(在 API>19 中以精确的时间间隔运行任务,请告诉我)非常感谢

【问题讨论】:

  • 检查此间接post 或直接post 以响应另一种方法
  • 不再精确了。该死的,我正在制作一个警报应用程序,这对我来说是个问题

标签: android alarmmanager intervals


【解决方案1】:

您可以像使用setRepeating 一样使用setExact

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);
}

这两个区别是:

  1. 时间将是准确的(或as nearly as possible
  2. 您必须在广播接收器的onReceive 中安排下一次发生。

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

【讨论】:

  • 但我是否需要设置 minSDKversion = 19 才能使用 setExact?
  • @MrNarendra 不,你不需要这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多