AlarmManager 有时不会发出警报
我正在为 Android 开发动态壁纸。为了在设定的时间刷新壁纸,我使用 AlarmManager。大多数情况下,这效果很好,但有时我的警报没有收到。最重要的是,我无法复制这种行为,它只是随机发生的。我至少使用 3 个 ROM 时遇到过这个问题。
现在是代码。
我使用这个 PendingIntent:
mRefreshIntent = new Intent()
.setComponent(new ComponentName(mContext, RefreshBroadcastReceiver.class))
.setAction("my.package.name.REFRESH_WALLPAPER");
mPendingRefreshIntent = PendingIntent.getBroadcast(
mContext,
0,
mRefreshIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
这是我设置闹钟的代码:
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, mPendingRefreshIntent);
其中时间是以毫秒为单位的 UTC 时间。我经常使用 adb shell dumpsys Alarm
验证警报是否按预期设置,事实确实如此。
接收方:
public class RefreshBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("DayNight", "onReceive ; " + System.currentTimeMillis());
DayNightService.refresher.refresh();
Log.d("DayNight", "onReceive done; " + System.currentTimeMillis());
}
}
关联清单行:
<application>
...
<receiver
android:name="RefreshBroadcastReceiver">
<intent-filter>
<action android:name="my.package.name.REFRESH_WALLPAPER" />
</intent-filter>
</receiver>
...
</application>
未触发的警报始终预先存在于队列中(dumpsys 警报),之后不会出现在警报日志中。看起来他们在 T 减零时“迷路”了。
如果你们中的一个人能为我解决这个问题,我将非常高兴。
I'm developing a live wallpaper for Android. To refresh the wallpaper at set times I use AlarmManager. Most of the times this works great, but occasionally my alarm isn't received. On top of that I can't replicate this behaviour, it just randomly happens. I've run into this using at least 3 ROMs.
Now for the code.
I use this PendingIntent:
mRefreshIntent = new Intent()
.setComponent(new ComponentName(mContext, RefreshBroadcastReceiver.class))
.setAction("my.package.name.REFRESH_WALLPAPER");
mPendingRefreshIntent = PendingIntent.getBroadcast(
mContext,
0,
mRefreshIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
This is my code to set the alarm:
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, mPendingRefreshIntent);
where time is the UTC time in milliseconds. I often verified if the alarm is set as intended using adb shell dumpsys alarm
, which it is.
The receiving side:
public class RefreshBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("DayNight", "onReceive ; " + System.currentTimeMillis());
DayNightService.refresher.refresh();
Log.d("DayNight", "onReceive done; " + System.currentTimeMillis());
}
}
Associated manifest lines:
<application>
...
<receiver
android:name="RefreshBroadcastReceiver">
<intent-filter>
<action android:name="my.package.name.REFRESH_WALLPAPER" />
</intent-filter>
</receiver>
...
</application>
Alarms which are not fired always exist in the queue (dumpsys alarms) beforehand, and are not in the alarm log afterwards. Seems like they get 'lost' at T minus zero.
I will be very happy if one of you can solve this problem for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用以下代码:
请注意,我请求不精确的重复闹钟和 RTC (不是 RTC_WAKEUP ) - 如果手机睡在牛仔裤口袋深处,用户对动态壁纸的更改不感兴趣 - 无需浪费电池电量和唤醒手机
您可能还需要注册启动完成广播接收器以开始更新计划
重新启动时。
I use following code:
Note, that I request inexact repeating alarm and RTC ( not RTC_WAKEUP ) - if phone is sleeping deep inside jeans pocket, user is not interested on changes of your live wallpaper - no need to waste battery juice and wake phone up
You may also need to register boot complete broadcast receiver to start update scheduling
on reboot.