如何检查AlarmManager是否设置了带有FLAG_ONE_SHOT的警报 [英] How do I check if AlarmManager has an alarm set with FLAG_ONE_SHOT

查看:75
本文介绍了如何检查AlarmManager是否设置了带有FLAG_ONE_SHOT的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建带有FLAG_ONE_SHOT的PendingIntent,则后续带有FLAG_NO_CREATE的PendingIntent返回null。

If I create a PendingIntent with FLAG_ONE_SHOT, a subsequent PendingIntent with FLAG_NO_CREATE returns null.

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context,AlarmService.class);
    PendingIntent pi = PendingIntent.getService(context,this.getId(),intent,PendingIntent.FLAG_ON_SHOT);
    GregorianCalendar alarmtime = new GregorianCalendar(now.get(GregorianCalendar.YEAR),now.get(GregorianCalendar.MONTH),now.get(GregorianCalendar.DAY_OF_MONTH),0,0);

    //Set the alarm
    if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT) {
        am.set(AlarmManager.RTC_WAKEUP,alarmtime.getTimeInMillis(), pi);
    } else {
        am.setExact(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pi);
    }

    //Now check if the alarm was set, if it was set, the following PendingIntent should return not null but it doesn't
    PendingIntent piCheck = PendingIntent.getService(context,this.getId(),intent,PendingIntent.FLAG_NO_CREATE);

    if (piCheck!=null) {
        Log.d(TAG,"piCheck returned NOT NULL and probably returned pi");
    } else if (piCheck==null) {
        Log.d(TAG,"piCheck returned NULL pi does not exist");

但是,如果我将第一个待处理的意图更改为:

However if I change the first pending intent to:

PendingIntent pi = PendingIntent.getService(context,this.getId(),intent,PendingIntent.FLAG_CANCEL_CURRENT);

然后我的第二个PendingIntent返回的值不为预期的空。

Then my second PendingIntent returns not null as expected.

两个PendingIntents都正确设置了警报,但是我无法检查 FLAG_ONE_SHOT PendingIntent。这种行为的原因是什么?

Both PendingIntents set an alarm properly, but I cannot "check" the FLAG_ONE_SHOT PendingIntent. What is the reason for this behaviour? What is the purpose of it?

推荐答案

我创建了一个小型测试程序来验证此行为。如果使用 FLAG_ONE_SHOT 创建 PendingIntent ,然后将其传递给 AlarmManager ,它看起来像Android立即消耗 PendingIntent ,因此它不再存在(因为它是一次性,因此只能使用一次)。我本以为会在实际触发警报时发生这种情况,但事实并非如此。

I created a small test program to verify this behaviour. If you create a PendingIntent using FLAG_ONE_SHOT and then pass this to the AlarmManager, it looks like Android "consumes" the PendingIntent immediately so that it no longer exists (because it is a "one-shot", it can only be used once). I would have thought this would happen when the alarm actually triggers, but it looks like that isn't the case.

您每天都会学到一些新东西:-)

You learn something new every day :-)

要解决您的问题,我将删除 FLAG_ONE_SHOT ,因为您可能不需要它(只需通过 0 作为标志参数)。如果您多次设置闹钟,则可以使用 FLAG_UPDATE_CURRENT 如果每次设置的闹钟都有不同的附加功能(但是您似乎没有使用任何附加功能,因此您可能不需要这个)。我认为您不需要 FLAG_ONE_SHOT 来完成您的工作。

To solve your problem, I would just remove the FLAG_ONE_SHOT as you probably don't need it (just pass 0 as the "flags" argument). If you set the alarm more than once, you can use FLAG_UPDATE_CURRENT if you set the alarm with different extras each time (but it looks like you aren't using any extras, so you probably don't need this). I don't think that you need FLAG_ONE_SHOT for what you are trying to do.

这篇关于如何检查AlarmManager是否设置了带有FLAG_ONE_SHOT的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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