我有两个按钮save and set alarm
和cancel alarm
,这些按钮可以完全按照他们的建议行事。
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
时显示
a http://www.4shared.com/download/puMWZEvRba/alert1.png
然而,由于我还有cancel
,我可以点击它来取消闹钟。所以我点击cancel
按钮就会显示
a http://www.4shared.com/download/1UOTyVK0ce/alert2.png
这似乎是正确的。但是当我再次单击save
按钮时,它会显示
a http://www.4shared.com/download/puMWZEvRba/alert1.png
这意味着cancel
按钮虽然为toast
执行了this alarm will be deleted.
,却没有做它应该做的事情。这再次意味着alarmManager.cancel(sender1)
必定存在问题。
问题
要在代码中修改哪些内容才能使cancel
按钮正常工作?
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);
此结果与之前相同。Cancel
按钮似乎不起作用。
解决方案
感谢 @David Wasser 我现在正在使用它。请看他的答案。我也不得不改变
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 indicating that if the described PendingIntent already exists, then simply return null instead of creating it.
我不知道这个问题!
答案 0 :(得分:4)
这里有几件事是错误的:
1请勿在{{1}}的通话中使用Intent.FILL_IN_DATA
。这是PendingIntent.getBroadcast()
标志,而不是Intent
标志。它不属于这里。
2当您使用PendingIntent
时,如果PendingIntent.FLAG_NO_CREATE
尚不存在,则会返回null
。在设置PendingIntent
的代码中,您已向后与alarmUp
进行比较。 注意:请参阅本答案末尾的评论,说明此文档的错误
3在null
你正在这样做:
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
中创建了PendingIntent
。
注意:当您拨打onCreate()
时,会创建PendingIntent
,而不会在您设置闹钟时创建。{/ p>
编辑:添加更多代码示例
如前所述,如果您想使用它来确定警报是否已设置,则无法创建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
的Android documentation似乎错了!它说:
表示如果描述的PendingIntent已存在的标志, 然后只返回null而不是创建它。
但这是倒退。如果PendingIntent.FLAG_NO_CREATE
已存在,此方法将返回PendingIntent
。 如果尚不存在,则会返回null
。
我编辑了我的答案,以反映此旗帜的正确操作。