如何检查 AlarmManager 是否已经设置了警报?

编程入门 行业动态 更新时间:2024-10-24 05:18:16
本文介绍了如何检查 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.

推荐答案

根据 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 **does not** 已经存在,那么只需返回 null(而不是创建一个新的)

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 是否已经设置了警报?

本文发布于:2023-11-27 10:21:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!