android – 警报“取消”按钮无法正常工作
作者:互联网
我有2个按钮保存并设置警报和取消警报,这意味着完全按照他们的建议.
在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);
当我第一次点击保存时显示
a http://www.4shared.com/download/puMWZEvRba/alert1.png
然而,因为我也取消了我可以点击它取消警报.所以我点击取消按钮,它显示
a http://www.4shared.com/download/1UOTyVK0ce/alert2.png
这似乎是正确的.但是,当我再次单击保存按钮时,它显示
a http://www.4shared.com/download/puMWZEvRba/alert1.png
这意味着取消按钮没有做它应该做的事情虽然它执行此警报的吐司将被删除..这再次意味着alarmManager.cancel(sender1)必定存在一些问题.
题
在代码中修改什么以使取消按钮正常工作?
P.S
我提到了像this这样的很多帖子,但在我的情况下无法得到确切的问题.
更新的代码
对于取消按钮
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);
结果与之前相同.取消按钮似乎不起作用.
解
感谢@David Wasser,我现在就开始工作了.请看他的回答.我也不得不改变
boolean alarmUp =(PendingIntent.getBroadcast(AlarmActivity.this,2,
警告,PendingIntent.FLAG_NO_CREATE)== null)
至
boolean alarmUp =(PendingIntent.getBroadcast(AlarmActivity.this,2,
两个块中的警报,PendingIntent.FLAG_NO_CREATE)!= null).
但是正如developer.android.com所说FLAG_NO_CREATE Flag表示如果描述的PendingIntent已经存在,那么只需返回null而不是创建它.我不知道这个问题!
解决方法:
这里有几件事是错的:
1不要在对PendingIntent.getBroadcast()的调用中使用Intent.FILL_IN_DATA.这是一个Intent标志,而不是PendingIntent标志.它不属于这里.
2当您使用PendingIntent.FLAG_NO_CREATE时,如果PendingIntent尚不存在,则返回null.在您设置alarmUp的代码中,您已经向后反对了null.注意:请参阅本答案末尾的评论,说明此文档的错误
3在你的onCreate()中,你这样做:
PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2,
alarmintent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
即使您没有使用它设置警报,此行也将创建PendingIntent.稍后,当您使用此代码检查PendingIntent是否存在时:
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,
alarmintent, PendingIntent.FLAG_NO_CREATE) == null);
alarmUp将始终为false,因为您已在onCreate()中创建了PendingIntent.
注意:PendingIntent是在调用PendingIntent.getBroadcast()时创建的,而不是在设置警报时创建的.
编辑:添加更多代码示例
正如我之前所说,如果要使用它来确定是否设置了警报,则无法创建PendingIntent.您必须首先检查PendingIntent是否存在,然后您可以创建它以设置/取消它.要修复,请执行以下操作:
在取消按钮中:
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的文档差异:
注意:看来关于PendingIntent.FLAG_NO_CREATE的Android documentation错了!它说:
Flag indicating that if the described PendingIntent already exists,
then simply return null instead of creating it.
但这是倒退的.如果PendingIntent已存在,则此方法将返回PendingIntent.如果它尚不存在,它将返回null.
我编辑了我的答案以反映这个标志的正确操作.
标签:android,android-alarms,android-pendingintent,alarmmanager 来源: https://codeday.me/bug/20191003/1846855.html