警报“取消"按钮无法正常工作 [英] Alarm "cancel" button not working correctly

查看:29
本文介绍了警报“取消"按钮无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个按钮 save and set alarmcancel alarm 用于完全按照他们的建议执行.

I have 2 buttons save and set alarm and cancel alarm which are meant to do exactly what they suggest.

内部 onCreate 声明变量

final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT |  Intent.FILL_IN_DATA);

取消按钮 onClickListener 中的代码

boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) == null);

if (alarmUp)
            {

                  new AlertDialog.Builder(AlarmActivity.this)
                  .setTitle("Alert")
                  .setMessage("This alarm will be deleted.")
                  .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface arg0, int arg1) {

                                        alarmManager.cancel(sender1);
                                        sender1.cancel();
                                        Toast.makeText(getApplicationContext(), "Alarm Cancelled.", Toast.LENGTH_LONG).show(); 
                                    }


                                })
                                .create().show();
                                }
                                else
                                {
                                    new AlertDialog.Builder(AlarmActivity.this)
                                    .setTitle("Alert")
                                    .setMessage("Alarm for this is not set yet.")
                                 .setPositiveButton("Ok",null)
                                .create().show();
                                }

保存按钮代码

boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2, 
                             alarmintent, 
                                PendingIntent.FLAG_NO_CREATE) == null);


                        if (alarmUp)
                        {
                            //Log.d("myTag", "Alarm is already active");
                            new AlertDialog.Builder(AlarmActivity.this)
                            .setTitle("Alert")
                            .setMessage("Already an alarm is set for this particular time and day.")
                            .setPositiveButton("OK",null    
                            )

                            .create().show();
                        }
    Calendar c = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis()+5000,24 * 60 * 60 * 1000, sender1);

当我第一次点击save时它显示

When i click save for the first time it shows

a http://www.4shared.com/download/puMWZEvRba/alert1.png

但是因为我也有cancel,所以我可以点击它来取消警报.所以我点击cancel按钮,它显示

However as i have cancel too i can click that to cancel the alarm.So i click cancel button and it shows

a http://www.4shared.com/download/1UOTyVK0ce/alert2.png

这看起来是对的.但是当我再次单击 save 按钮时,它显示

which seems right.But when i again click save button it shows

a http://www.4shared.com/download/puMWZEvRba/alert1.png

这意味着 cancel 按钮没有做它应该做的事情,尽管它执行了 toast 对于 这个警报将被删除..哪个再次表示 alarmManager.cancel(sender1) 肯定有问题.

which means the cancel button is not doing what it should do although it executes the toast for this alarm will be deleted..Which again means there must be some problem with alarmManager.cancel(sender1).

问题

要在代码中修改哪些内容才能使 cancel 按钮正常工作?

What to modify in the code to get the cancel button work correctly?

附言

我参考了很多帖子,例如 this 但不知道是什么就我而言,确切的问题是.

I referred many posts like this but can't get what's the exact problem in my case.

更新代码

对于取消按钮

final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
    final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
    boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) == null);

if (alarmUp)
            {

                  new AlertDialog.Builder(AlarmActivity.this)
                  .setTitle("Alert")
                  .setMessage("This alarm will be deleted.")
                  .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface arg0, int arg1) {

                                        alarmManager.cancel(sender1);
                                        sender1.cancel();
                                        Toast.makeText(getApplicationContext(), "Alarm Cancelled.", Toast.LENGTH_LONG).show(); 
                                    }


                                })
                                .create().show();
                                }
                                else
                                {
                                    new AlertDialog.Builder(AlarmActivity.this)
                                    .setTitle("Alert")
                                    .setMessage("Alarm for this is not set yet.")
                                 .setPositiveButton("Ok",null)
                                .create().show();
                                }   

用于保存按钮

 final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
                        final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                        final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);

boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2, 
                             alarmintent, 
                                PendingIntent.FLAG_NO_CREATE) == null);


                        if (alarmUp)
                        {
                            //Log.d("myTag", "Alarm is already active");
                            new AlertDialog.Builder(AlarmActivity.this)
                            .setTitle("Alert")
                            .setMessage("Already an alarm is set for this particular time and day.")
                            .setPositiveButton("OK",null    
                            )

                            .create().show();
                        }
    Calendar c = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis()+5000,24 * 60 * 60 * 1000, sender1);

这与之前的结果相同.Cancel 按钮似乎不起作用.

This results in same as previous.Cancel button doesn't seem to work.

解决方案

感谢 @David Wasser 我现在可以使用了.请看他的回答.我也不得不改变

Thanks to @David Wasser i got it working now.Please see his answer.I also had to change

boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) == null)

boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) != null) 在两个块中.

但正如 developer.android.com 所说FLAG_NO_CREATE Flag 表示如果描述的 PendingIntent 已经存在,那么只需返回 null 而不是创建它.我不知道这个问题!

But as developer.android.com says FLAG_NO_CREATE Flag indicating that if the described PendingIntent already exists, then simply return null instead of creating it.I don't know about this issue!

推荐答案

这里有几个错误:

1 不要在对 PendingIntent.getBroadcast() 的调用中使用 Intent.FILL_IN_DATA.这是一个 Intent 标志而不是 PendingIntent 标志.它不属于这里.

1 Don't use Intent.FILL_IN_DATA in the call to PendingIntent.getBroadcast(). This is an Intent flag and not a PendingIntent flag. It doesn't belong here.

2 当您使用 PendingIntent.FLAG_NO_CREATE 时,如果 PendingIntent 不存在,这将返回 null.在您设置 alarmUp 的代码中,您已经对 null 进行了反向比较.注意:请参阅我在此答案末尾的评论,关于此文档是错误的这一事实

2 When you use the PendingIntent.FLAG_NO_CREATE this will return null if the PendingIntent doesn't already exist. In your code to set alarmUp you've got the comparison against null backwards. NOTE: See my comments at the end of this answer about the fact that the documentation for this is wrong

3 在您的 onCreate() 中,您正在这样做:

3 In your onCreate() you are doing this:

PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2,
      alarmintent, PendingIntent.FLAG_UPDATE_CURRENT |  Intent.FILL_IN_DATA);

这一行将创建 PendingIntent 即使你没有用它设置闹钟.稍后,当您使用此代码检查 PendingIntent 是否存在时:

This line will create the PendingIntent even if you don't set an alarm with it. Later, when you check if the PendingIntent exists with this code:

boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2, 
      alarmintent, PendingIntent.FLAG_NO_CREATE) == null);

alarmUp 将始终为 false,因为您已经在 onCreate() 中创建了 PendingIntent.

alarmUp will always be false, because you have already created the PendingIntent in onCreate().

注意:PendingIntent 是在您调用 PendingIntent.getBroadcast() 时创建的,而不是在您设置闹钟时创建.

NOTE: The PendingIntent is created when you call PendingIntent.getBroadcast(), not when you set the alarm.

添加更多代码示例

正如我之前所说的,如果您想使用 PendingIntent 来确定是否设置了闹钟,则无法创建它.您必须首先检查 PendingIntent 是否存在,然后您可以创建它来设置/取消它.要修复,请执行以下操作:

As I said earlier, you can't create the PendingIntent if you want to use it to determine whether the alarm is set or not. You must first check if the PendingIntent exists and then you can create it to set/cancel it. To fix, do this:

取消按钮:

final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// Determine if the alarm has already been set
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp) {
    final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
    ...

保存按钮:

final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
// Determine if the alarm has already been set
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2, alarmintent, PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp) {
    final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
    ...

<小时>

再次编辑以修复与 PendingIntent.FLAG_NO_CREATE 的文档差异:


EDITED again to fix documentation discrepancy with PendingIntent.FLAG_NO_CREATE:

注意:似乎Android 文档关于PendingIntent.FLAG_NO_CREATE 错了! 它说:

Note: It seems the Android documentation about PendingIntent.FLAG_NO_CREATE is wrong! It says:

Flag 表示如果描述的 PendingIntent 已经存在,然后简单地返回 null 而不是创建它.

Flag indicating that if the described PendingIntent already exists, then simply return null instead of creating it.

但这是倒退.如果 PendingIntent 已经存在,这个方法将返回它.如果不存在则返回null.

but this is backwards. This method will return the PendingIntent if it already exists. It will return null if it doesn't already exist.

我已经编辑了我的答案以反映此标志的正确操作.

I've edited my answer to reflect the correct operation of this flag.

这篇关于警报“取消"按钮无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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