当我的应用程序启动时,我想让它检查一个特定的警报(通过AlarmManager注册)是否已经设置并运行。谷歌的结果似乎表明没有办法做到这一点。这仍然正确吗?我需要做这个检查,以便在采取任何行动创建一个新的警报之前建议用户。


当前回答

对于其他可能需要这个的人,这里有一个答案。

使用adb shell dumpsys警报

你可以知道闹铃已经设置好了,什么时候闹铃和间隔时间。还有这个警报被触发的次数。

其他回答

我有两个闹钟。我使用带有额外内容的意图而不是行动来识别事件:

Intent i = new Intent(context, AppReciever.class);
i.putExtra("timer", "timer1");

问题是,不同的额外的意图(和警报)不会是唯一的。因此,为了能够识别哪个警报是活跃的或不活跃的,我必须定义diff requestCode-s:

boolean alarmUp = (PendingIntent.getBroadcast(context, MyApp.TIMER_1, i, 
                    PendingIntent.FLAG_NO_CREATE) != null);

下面是如何创建警报的:

public static final int TIMER_1 = 1;
public static final int TIMER_2 = 2;

PendingIntent pending = PendingIntent.getBroadcast(context, TIMER_1, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending);
pending = PendingIntent.getBroadcast(context, TIMER_2, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
setInexactRepeating(AlarmManager.RTC_WAKEUP,
            cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending);

刚找到另一个解决办法,似乎对我有用

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

boolean isWorking = (PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_NO_CREATE) != null);
if (isWorking) {Log.d("alarm", "is working");} else {Log.d("alarm", "is not working");}

if(!isWorking) {
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,    PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    int timeNotif = 5 * 60 * 1000;//time in ms, 7*24*60*60*1000 for 1 week
    Log.d("Notif", "Notification every (ms): " + timeNotif);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), timeNotif, pendingIntent);
    }

虽然这里几乎每个人都给出了正确答案,但没有人解释警报的工作依据是什么

你可以在这里了解更多关于AlarmManager及其工作的信息。但这里有一个简单的答案

你可以看到AlarmManager基本上在未来的某个时间安排一个PendingIntent。所以为了取消预定的Alarm,你需要取消PendingIntent。

在创建PendingIntent时,总是要注意两件事

PendingIntent.getBroadcast(context,REQUEST_CODE,intent, PendingIntent.FLAG_UPDATE_CURRENT);

请求代码——作为唯一标识符 Flag -定义PendingIntent的行为

现在要检查警报是否已经被安排或取消警报,你只需要访问相同的PendingIntent。如果您使用相同的请求代码并使用如下所示的FLAG_NO_CREATE,就可以做到这一点

PendingIntent pendingIntent=PendingIntent.getBroadcast(this,REQUEST_CODE,intent,PendingIntent.FLAG_NO_CREATE);

if (pendingIntent!=null)
   alarmManager.cancel(pendingIntent);

使用FLAG_NO_CREATE,如果PendingIntent不存在,它将返回null。如果它已经存在,则返回对现有的PendingIntent的引用

请注意Alarm Manager的set方法的文档引用:

如果已经为此意图计划了一个警报(使用 由Intent.filterEquals定义的两个意图的相等性),然后它 将被移除,并被这个替换。

如果你知道你想要闹钟设置,那么你不需要费心检查它是否已经存在。每次你的应用启动时创建它。您将用相同的意图替换任何过去的警报。

如果您试图计算以前创建的警报还剩多少时间,或者您确实需要知道这样的警报是否存在,则需要使用不同的方法。要回答这些问题,请考虑在创建警报时保存共享pref数据。您可以存储闹钟设置时刻的时钟时间戳,您希望闹钟响起的时间,以及重复周期(如果您设置了重复闹钟)。

对于其他可能需要这个的人,这里有一个答案。

使用adb shell dumpsys警报

你可以知道闹铃已经设置好了,什么时候闹铃和间隔时间。还有这个警报被触发的次数。