Android的AlarmManager问题设置和放大器;重置警报

编程入门 行业动态 更新时间:2024-10-25 16:27:29
本文介绍了Android的AlarmManager问题设置和放大器;重置警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用警报从服务器获取数据。 我喜欢给用户来启动和停止警报的选项。 这意味着我要检查,看看闹钟已经设置。 我发现了一些code,告诉我,如果报警已经设为:

I use an Alarm to fetch data from server. I like to give user the option to start and stop the alarm. This means I have to check and see if alarm is already set. I found some code that tells me if the alarm is already set:

Intent I = new Intent(getApplicationContext(),AlarmReceiver.class); PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), 0, I, PendingIntent.FLAG_NO_CREATE); found = (P!=null);

如果警报已经设置我取消它,但如果它没有设置,然后我把它(如切换)

if the Alarm is already set I cancel it but if it is not set then I set it (like a toggle)

问题是这样的工作只有一次。第一次上code,检查现有的报警 将返回null,表示没有报警,但之后我取消报警,一旦它返回一个指针 到东西,但警报未运行。

Problem is this works only once. The first time the above code to check existing alarms will return null indicating no alarm but after I cancel the alarm once it returns a pointer to something but alarm is not running.

这里是code设置报警

here is the code to set alarm

am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE); Intent I = new Intent(getApplicationContext(),AlarmReceiver.class); PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), 0, I, PendingIntent.FLAG_CANCEL_CURRENT); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, P);

和这里是code取消报警:

and here is the code to cancel an alarm:

am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE); Intent I = new Intent(getApplicationContext(),AlarmReceiver.class); PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), 0, I, PendingIntent.FLAG_CANCEL_CURRENT); am.cancel(P);

我是要取消报警,使它的PendingIntent消失后复位东西。

Am I to reset something after canceling an alarm to make it's PendingIntent go away.

推荐答案

在取消 AlarmManager 不要使用 PendingIntent 与 FLAG_CANCEL_CURRENT 。 相反,取消 PendingIntent 取消报警后明确:

When canceling the AlarmManager do not use a PendingIntent with a flag of FLAG_CANCEL_CURRENT. Instead, cancel the PendingIntent explicitly after canceling the alarm:

am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE); Intent i = new Intent(getApplicationContext(),AlarmReceiver.class); PendingIntent p = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0); am.cancel(p); p.cancel();

更多推荐

Android的AlarmManager问题设置和放大器;重置警报

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

发布评论

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

>www.elefans.com

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