我有2个按钮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
按钮正常工作?压力
我提到了许多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.
,我不知道这个问题! 最佳答案
这里有几处错误:
1不要在对Intent.FILL_IN_DATA
的调用中使用PendingIntent.getBroadcast()
。这是一个Intent
标志,而不是PendingIntent
标志。它不属于这里。
2使用PendingIntent.FLAG_NO_CREATE
时,如果null
不存在,它将返回PendingIntent
。在设置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
,因为您已经在PendingIntent
中创建了onCreate()
。注意:
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似乎是错误的!它说:标记,表明所描述的PendingIntent已经存在,
然后简单地返回null而不是创建它。
但这是倒退。如果
PendingIntent
已经存在,此方法将返回。如果尚不存在,它将返回null
。我已经编辑了答案以反映此标志的正确操作。