AlarmManager 有时不会发出警报

发布于 2024-12-20 04:47:06 字数 1675 浏览 4 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2024-12-27 04:47:06

我使用以下代码:

  Intent intent = new Intent(ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
    Log.d(LOG_TAG, "pending intent: " + pendingIntent);
    // if no intent there, schedule it ASAP
    if (pendingIntent == null) {
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        // schedule new alarm in 15 minutes
        alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent);
        Log.d(LOG_TAG, "scheduled intent: " + pendingIntent);
    }

请注意,我请求不精确的重复闹钟和 RTC (不是 RTC_WAKEUP ) - 如果手机睡在牛仔裤口袋深处,用户对动态壁纸的更改不感兴趣 - 无需浪费电池电量和唤醒手机

您可能还需要注册启动完成广播接收器以开始更新计划
重新启动时。

I use following code:

  Intent intent = new Intent(ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
    Log.d(LOG_TAG, "pending intent: " + pendingIntent);
    // if no intent there, schedule it ASAP
    if (pendingIntent == null) {
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        // schedule new alarm in 15 minutes
        alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent);
        Log.d(LOG_TAG, "scheduled intent: " + pendingIntent);
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文