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

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

问题描述

我有2个按钮保存设置报警取消报警这意味着可以做的正是他们建议。

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

code内取消按钮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();
                                }

code的保存按钮

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

当我点击保存首次就说明

不过,因为我有取消过,我可以单击取消alarm.So我点击取消按钮,它显示了

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

这似乎right.But当我再次点击保存按钮,它显示了

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

这意味着取消按钮没有做它应该做的,尽管它执行面包此警报将被删除。。其中又意味着必须有一些问题, 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).

问题

怎么在code修改以获得取消按钮正常工作?

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

P.S

我提到很多帖子如但不能得到什么在我的情况具体问题。

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

更新code

对于取消按钮

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

这导致在相同previous。取消按钮似乎没有工作。

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

解决方案

由于 @大卫瓦瑟我得到它的工作now.Please看到他answer.I也不得不改变

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

布尔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)

布尔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) in both the blocks.

但是,随着 developer.android.com FLAG_NO_CREATE标志,指示,如果所描述的PendingIntent已经存在,则简单地返回创建它的空吧。我不知道这个问题!

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,不要使用 Intent.FILL_IN_DATA 在调用 PendingIntent.getBroadcast()。这是一个意图标记,而不是 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 不存在。在您的code设置 alarmUp 你有对比较落后。 注意:在有关的事实这个答案的最后见我的评论说,这个文件是错误的

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()你在做这样的:

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

这条线将创建 PendingIntent ,即使您没有设置报警吧。稍后,当您检查 PendingIntent 此code存在:

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 永远是,因为你已经创建了 PendingIntent 的onCreate()

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.

编辑:添加更多code例子

正如我刚才所说,你不能创建,如果你想用它来判断报警是否设置与否 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:

注:看来的Andr​​oid文档 PendingIntent.FLAG_NO_CREATE 是错上面写着:

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

标志指示如果所述PendingIntent已经存在,   然后简单地返回创建它的空吧。

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

不过,这是倒退。该方法将返回 PendingIntent 如果它已经存在。 它会返回,如果它不存在

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天全站免登陆