Android 有没有办法检查是否已经设置了闹钟?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10930034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:30:49  来源:igfitidea点击:

Is there any way to check if an alarm is already set?

androidalarmmanager

提问by NullPointerException

I am stuck. When my application starts I want to check if an alarm is alive that I previously set. If not then I want to set it.

我被困住了。当我的应用程序启动时,我想检查我之前设置的警报是否处于活动状态。如果没有,那么我想设置它。

I referred to this solution. I am trying to match the intent like this:

我提到了这个解决方案。我试图匹配这样的意图:

Intent intent = new Intent();
        intent.setAction("com.vit.upload");
        PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);

But it doesn't work. What else would work?

但它不起作用。还有什么工作?

回答by Erol

First of all, a little tutorial on how to access previously created alarms:

首先,关于如何访问以前创建的警报的小教程:

You can differentiate between alarms by creating each with a unique id such as:

您可以通过创建具有唯一 ID 的警报来区分警报,例如:

Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,UNIQUE_ID_GOES_HERE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis ,pi);

When you want to access this alarm, you have to create the same PendingIntent with the same unique id. For example, the following will only access an alarm that you created with PendingIntent id 1234. Then it will cancel the previous one and reset it.

当您想要访问此警报时,您必须创建具有相同唯一 id 的相同 PendingIntent。例如,以下将仅访问您创建的 PendingIntent id 为 1234 的警报。然后它将取消前一个并重置它。

Intent intent = new Intent(this, AlarmReceiverActivity.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 1234, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis ,pi);

The idea is simple. Keep track of the id's and then use them to access their respective alarms. If you create multiple alarms with same id, the most recent one will cancel the previous.

这个想法很简单。跟踪 ID,然后使用它们访问各自的警报。如果您创建多个具有相同 id 的闹钟,最近的一个将取消前一个。

Coming to your main problem, instead of checking if your alarm is active each time you launch your application, just re-set it in your Activity's onCreate()method. with the same PendingIntent as I described above. This saves you all the hassle of checking if the alarm is previously set or not. Since your aim is to keep the alarm alive, it won't hurt to override the previously set one everytime you launch the application. Just make sure you use the same id to create your PendingIntent.

来到您的主要问题,而不是每次启动应用程序时检查您的警报是否处于活动状态,只需在您的活动onCreate()方法中重新设置它。具有与我上面描述的相同的 PendingIntent。这可以为您省去检查警报是否预先设置的所有麻烦。由于您的目标是使警报保持活动状态,因此每次启动应用程序时覆盖先前设置的警报不会有什么坏处。只要确保您使用相同的 ID 来创建您的 PendingIntent。

Do not forget to check if the time for your alarm has already passed or not in order to avoid trying to set an alarm for a past time, which will trigger it immediately.

不要忘记检查您的闹钟时间是否已经过去,以避免尝试将闹钟设置为过去的时间,这会立即触发它。

Let us consider another case: when you turn off your device, all your alarms will be cancelled. This leaves you no option but to set them again at reboot. To do that, you will have to use a BroadcastReceiver.

让我们考虑另一种情况:当您关闭设备时,您的所有闹钟都将被取消。这让您别无选择,只能在重新启动时再次设置它们。为此,您必须使用BroadcastReceiver

This answerwill help you on how to do that. Just recreate your alarm in the onReceive() method of your BroadcastReceiver as suggested above.

这个答案将帮助您了解如何做到这一点。只需按照上面的建议在您的 BroadcastReceiver 的 onReceive() 方法中重新创建您的警报。

回答by Munish Kapoor

  • First alarm will not work when you reboot your android device.
  • You can use the boolean shared preferences to check the alarm is created or not.
  • Android pending intent use the Unique ID

    int REQUEST_CODE=2344; // Unique ID  
    PendingIntent pIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_NO_CREATE);
  • 当您重新启动 android 设备时,第一个警报将不起作用。
  • 您可以使用布尔共享首选项来检查警报是否创建。
  • Android 待处理意图使用唯一 ID

    int REQUEST_CODE=2344; // Unique ID  
    PendingIntent pIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_NO_CREATE);

you have to use the Boot Receiver broadcast when system reboot then create again alaram with the same request code REQUEST_CODE=2344;

您必须在系统重新启动时使用 Boot Receiver 广播,然后使用相同的请求代码再次创建闹钟 REQUEST_CODE=2344;

回答by Jakub Szczygie?

Basiclly from my experiance if you use the same Intent and FLAG_UPDATE_CURRENT, you can be sure that you won't have two alarm set for the same intent. Also you can look closely at FLAG_NO_CREATEwhich is used with get functions and returns null if pendingintent with described intent already exists.

基本上,根据我的经验,如果您使用相同的 Intent 和FLAG_UPDATE_CURRENT,您可以确定不会为相同的意图设置两个警报。您还可以仔细查看FLAG_NO_CREATE,它与 get 函数一起使用,如果具有描述意图的待定意图已经存在,则返回 null。

Also remember to use the same request id for pending intent as their are distinguishable.

还要记住对挂起的意图使用相同的请求 ID,因为它们是可区分的。

回答by Eliezer

Based on the information here, there does not seem to be a call you could make to directly check whether an alarm exists or not, but they do offer some workarounds.

根据此处的信息,您似乎无法拨打电话来直接检查警报是否存在,但它们确实提供了一些解决方法。