如何检查是否AlarmManager已经有一个报警设置? [英] How to check if AlarmManager already has an alarm set?

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

问题描述

在我的应用程序开始,我希望它检查是否有特定的报警(通过AlarmManager注册)已经被设置和运行。结果来自谷歌似乎表明,有没有办法做到这一点。这仍然是正确的吗?我需要做这个检查,以提醒用户在采取任何行动之前创建一个新的警报。 谢谢 罗恩

When my app starts I want it to check if a particular alarm (registered via AlarmManager) is already set and running. Results from google seem to indicate that there is no way to do this . Is this still correct? I need to do this check in order to advise the user before any action is taken to create a new alarm. Thanks Ron

推荐答案

在Ron张贴评论跟进,这里是详细的解决方案。比方说,你已经注册了一个悬而未决的意图这样的重复报警:

Following up on the comment ron posted, here is the detailed solution. Let's say you have registered a repeating alarm with a pending intent like this:

Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, 
                                      intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent);

您会检查,看它是否有效的方法是:

The way you would check to see if it is active is to:

boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
        new Intent("com.my.package.MY_UNIQUE_ACTION"), 
        PendingIntent.FLAG_NO_CREATE) != null);

if (alarmUp)
{
    Log.d("myTag", "Alarm is already active");
}

这里的关键是 FLAG_NO_CREATE 这是在Javadoc描述:如果描述PendingIntent **不**已经存在,那么只需返回(而不是创建一个新的)

The key here is the FLAG_NO_CREATE which as described in the javadoc: if the described PendingIntent **does not** already exists, then simply return null (instead of creating a new one)

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

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