【问题标题】:Scheduled alarms are not firing properly预定的警报未正确触发
【发布时间】:2013-02-19 10:05:23
【问题描述】:

我的应用程序为特定时间安排了多个警报。这些警报是在应用程序启动时安排的。 (每天有 5 个警报,每周有 35 个警报)...

我已通过日志验证这些警报是在应用程序启动时安排的。

问题是当我开始测试我的应用程序时,7 个警报完全正常。然而,第 8 个警报没有响起。我已经通过让我的设备静止超过 1 天来测试这种情况。如何调试此行为以及阻止触发警报的可能原因是什么。

编辑:

调度代码:

try {
    if (info != null) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, info.getHour());
        c.set(Calendar.MINUTE, info.getMinute());
        c.set(Calendar.SECOND, 0);

        Intent intent = new Intent(context, AlarmReceiverActivity.class);
        intent.putExtra("name", info.getPrayerName());
        intent.putExtra("sound", soundType);

        intent.putExtra("time", formatTimeClock(context, info.getHour(), info.getMinute()));

        PendingIntent pendingIntent = PendingIntent.getActivity(context, alarmId, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
    }
} catch (Exception e) {
    Log.e("ALarmSchedularManager", e.getMessage());
}

【问题讨论】:

  • 您能否显示您的 AlarmManager setRepeating 代码部分
  • 定时报警的共享代码。

标签: android alarmmanager android-alarms repeatingalarm


【解决方案1】:

如何调试这种行为

使用adb shell dumpsys alarm 查看您的预定警报是什么以及下次调用它们的时间。

阻止触发警报的可能原因是什么

您的代码没有考虑到时间已经过去的可能性,尽管这可能在您上面显示的代码 sn-p 之外处理。

【讨论】:

  • 谢谢,你能帮我理解系统dumpsys: RTC_WAKEUP #723: Alarm{41d63e98 type 0 com.example} type=0 when=+3d11h33m50s418ms repeatInterval=0 count=0 operation=PendingIntent {41882d80:PendingIntentRecord{41d63df8 com.athan startActivity}} 如果我错了,请纠正我:#723:警报 ID 时间:从基准开始的毫秒数。 repeatInterval:这不是重复警报
  • @Maverick:“何时”表示您的闹钟将在您运行 @ 之后的三天十一点三十三分五十秒四百一十八毫秒后响起987654323@ 命令。
  • 顺便说一句,如果您也可以看看:stackoverflow.com/q/14952076/1053097 我很难开发用于发送网络请求的故障安全队列。
【解决方案2】:

希望下面的代码会有所帮助,我在我的应用程序中使用了相同的代码。这里在 AlarmManager 类中传递的用于重复的参数应该是 24*60*60*1000

AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);         
Intent intent1 = new Intent(ct, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);

Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);

Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),24*60*60*1000, pendingIntent);

您可以使用 AlarmManager.INTERVAL_DAY 而不是硬代码操作 (24*60*60*1000)。检查这个不同的间隔,最多 1 天 http://developer.android.com/reference/android/app/AlarmManager.html#INTERVAL_DAY

【讨论】:

  • 谢谢,但我不能在这里使用 setRepeating,因为用户可以随时更新各个警报。
  • 你可以。只需存储您用于设置警报的 Intent,如果用户更改警报,请使用 am.cancel(mPendingIntent)。然后设置新的。
  • 这应该是你的方式,顺便说一句,使用 setRepeating with inexact Intervals。否则你的应用会消耗大量电量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 2022-08-02
相关资源
最近更新 更多