有没有什么办法来检查,如果报警已经设置?有没有什么、办法

2023-09-12 08:51:06 作者:狂野的人哼著狂想曲#

我卡住了。当我的应用程序开始我要检查,如果报警还活着,我previously设置。如果没有,那么我想设置它。

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.

我称之为this解决方案。我想匹配这样的意图:

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?

推荐答案

首先,关于如何访问previously创建警报一小教程:

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

您可以报警之间通过一个唯一的ID,如创建每个分化

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

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);

当您要访问此报警,你必须创建相同的PendingIntent具有相同的唯一ID。例如,下面将只访问您与PendingIntent ID创建一个报警1234。然后,它会取消previous之一,并重新设置。

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.

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);

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

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.

来你的主要问题,而不是检查,如果你的报警器被激活每次启动应用程序时,只需在您的活动的的onCreate()方法重新设置。与同PendingIntent如我上面描述。这样可以节省你检查是否报警previously设置与否的所有麻烦。因为你的目标是保持报警活着,也不会伤到覆盖previously设置一个每次你启动应用程序。只要确保你用相同的ID创建PendingIntent。

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.

不要忘记检查,如果时间为您的报警已经通过与否,以避免试图设置警报过去的时间,这将立即触发。

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.

让我们考虑另一种情况:当你关闭你的设备,所有的报警将被取消。这使你没有选择,只能在重新启动时重新设置。要做到这一点,你将不得不使用一个BroadcastReceiver.

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的的onReceive()方法,你的闹钟按照上面的建议。

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