报警"取消]按钮无法正常工作[解决]

编程入门 行业动态 更新时间:2024-10-26 15:21:09
本文介绍了报警"取消]按钮无法正常工作[解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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 说 FLAG_NO_CREATE标志,指示,如果所描述的PendingIntent已经存在,则简单地返回创建它的空吧。我不知道这个问题!

But as developer.android 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.

更多推荐

报警"取消]按钮无法正常工作[解决]

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

发布评论

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

>www.elefans.com

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