我有一个警报管理器,我想查看警报是否已设置,以及是否显示了一个不同的警报对话框,如果尚未设置,则显示为已关闭。我找到了一些我被告知应该工作的代码,但它不是。此外,如果已设置警报,警报对话框应提供取消警报的选项,但也不起作用。这是我的代码:
boolean alarmUp = (PendingIntent.getBroadcast(Main.this, 1,
new Intent(Main.this, Alarm_Receiver.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp){
//Sets up the custom alert dialog layout gathers the selected time for the notifications to be set
LayoutInflater factory = LayoutInflater.from(this);
final View time_picker = factory.inflate(R.layout.dialog_layout2, null);
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(this);
alertDialogBuilder2.setView(time_picker);
alertDialogBuilder2.setTitle("Select Your Notification Time:");
alertDialogBuilder2
.setMessage("Please select the time when you want the notifications to appear on your phone each day. Please note: If your phone restarts or runs out of battery you will have to reset the time to receive the notifications.")
.setCancelable(false)
.setPositiveButton("SET TIME",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
TimePicker timePicker = (TimePicker) time_picker.findViewById(R.id.time_picker);
timePicker.clearFocus();
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
Intent intentAlarm = new Intent(Main.this, Alarm_Receiver.class);
intentAlarm.setAction("com.raddevelopment.organiseyourbreeding_pro.CUSTOM_INTENT");
PendingIntent pendingIntent = PendingIntent.getBroadcast(Main.this,1, intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(Main.this, "Alarm Scheduled", Toast.LENGTH_LONG).show();
return;
}
});
alertDialogBuilder2.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Exits
}
});
AlertDialog alertDialog2 = alertDialogBuilder2.create();
alertDialog2.show();
}else{
//Sets up the alert dialog
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(this);
// set title of alert dialog
alertDialogBuilder2.setTitle("Scheduled Notifications are Currently Set:");
// set dialog message
alertDialogBuilder2
.setMessage("Would you like to cancel these notifications?")
.setCancelable(false)
.setPositiveButton("YES",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = (PendingIntent.getBroadcast(Main.this, 1,
new Intent(Main.this, Alarm_Receiver.class),
PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.cancel(pi);
pi.cancel();
}
})
.setNegativeButton("NO",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
});
// create alert dialog
AlertDialog alertDialog2 = alertDialogBuilder2.create();
// show it
alertDialog2.show();
}
答案 0 :(得分:7)
为了确定是否设置了警报,您执行此操作:
boolean alarmUp = (PendingIntent.getBroadcast(Main.this, 1,
new Intent(Main.this, Alarm_Receiver.class),
PendingIntent.FLAG_NO_CREATE) != null);
但是当你设置闹钟时,你会这样做:
Intent intentAlarm = new Intent(Main.this, Alarm_Receiver.class);
intentAlarm.setAction("com.raddevelopment.organiseyourbreeding_pro.CUSTOM_INTENT");
PendingIntent pendingIntent = PendingIntent.getBroadcast(Main.this,1, intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
这些Intent
不匹配。您需要在正在使用的Intent
上设置ACTION以确定是否设置了警报。像这样:
boolean alarmUp = (PendingIntent.getBroadcast(Main.this, 1,
new Intent(Main.this, Alarm_Receiver.class)
.setAction("com.raddevelopment.organiseyourbreeding_pro.CUSTOM_INTENT"),
PendingIntent.FLAG_NO_CREATE) != null);