创建然后取消多个报警与AlarmManager设置 [英] Creating then Canceling multiple alarms set with AlarmManager

查看:172
本文介绍了创建然后取消多个报警与AlarmManager设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户挑选分钟池。因此,他们可能会选择5,15,30和50分钟。然后,alarmmanager警报设置为那些同时具有以下code的每一个(code的这块伟大工程):

I have an app that allows the user to pick a pool of minutes. So, they may pick 5, 15, 30 and 50 minutes. Then, an alarmmanager alarm is set for each one of those at the same time with the following code (this block of code works great):

//SET ALL ALARMS
                for(int i = 0; i < spAlarms.length; i++) {
                    try {
                        if(!spAlarms[i].equals("")) {
                            int time = Integer.valueOf(spAlarms[i]);
                            final int intent_id = (int) System.currentTimeMillis();
                            /*
                            Calendar cal = Calendar.getInstance();
                            cal.add(Calendar.MINUTE, time);
                            */
                            Intent intent = new Intent(Hop_Timer.this, Alarm_Receiver.class);
                            intent.putExtra("alarm_message", "Time for your " + spAlarms[i] + " min addition!");
                            intent_ids += "intent_id;";
                            PendingIntent sender = PendingIntent.getBroadcast(Hop_Timer.this, intent_id, intent, PendingIntent.FLAG_ONE_SHOT);
                            AlarmManager am = (AlarmManager) Hop_Timer.this.getSystemService(Context.ALARM_SERVICE);
                            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time*1000), sender);
                            //Toast.makeText(getApplicationContext(), "Set alarm" + String.valueOf(System.currentTimeMillis() + (time*1000)), Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                //ALARMS SET

我希望用户能够清除所有警报并重新启动,甚至将它们设置,打去后。我想了AlarmManager.cancel类,但它不工作。文档说,如果意图是一样的用来设置报警,然后它会取消它的意图。所以,我用code此块将其取消:

I want the user to be able to clear all the alarms and start over, even after setting them and hitting go. I'm trying the AlarmManager.cancel class but it isn't working. The documentation says if the intent is the same as the intent used to set the alarm then it will cancel it. So, I use this block of code to cancel it:

String[] spCANCELS = intent_ids.split(";");

                //Cancel all previous set alarms
                for(int i = 0; i < spCANCELS.length; i++) {
                    try {
                        if(!spCANCELS[i].equals("")) {
                            int time = Integer.valueOf(spCANCELS[i]);
                            Intent intent = new Intent(Hop_Timer.this, Alarm_Receiver.class);
                            intent.putExtra("alarm_message", "Time for your " + spCANCELS[i] + " min addition!");
                            PendingIntent sender = PendingIntent.getBroadcast(Hop_Timer.this, time, intent, PendingIntent.FLAG_ONE_SHOT);
                            AlarmManager am = (AlarmManager) Hop_Timer.this.getSystemService(Context.ALARM_SERVICE);
                            am.cancel(sender);
                        }
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                }
                //All clear!

唉,报警滚滚而来。我通过5秒投入10个不同的报警分开测试。我让第3或4个报警熄灭,然后打我的取消按钮,但报警滚滚而来(当所有可在点击里面,野清晰,所以我知道点击监听器正在工作)。

Alas, the alarms keep coming. I test by putting in 10 different alarms at 5 seconds apart. I let the first 3 or 4 alarms go off then hit my cancel button but the alarms keep coming (while all the field clear that are inside the click, so I know the click listener is working).

编辑:没关系,我需要多加注意找过我的语法时。这条线:intent_ids + =intent_id;不应该有变量intent_id在引号中。

Nevermind, I need to pay more attention when looking over my syntax. This line: intent_ids += "intent_id;"; should not have had the variable intent_id in quotes.

推荐答案

没关系,我需要多加注意找过我的语法时。这条线:intent_ids + =intent_id;不应该有变量intent_id在引号中。

Nevermind, I need to pay more attention when looking over my syntax. This line: intent_ids += "intent_id;"; should not have had the variable intent_id in quotes.

这篇关于创建然后取消多个报警与AlarmManager设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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