我有一个警报管理器,我想在其中查看警报是否已设置,如果尚未设置,它是否显示不同的警报对话框作为并列的 ti。我发现了一些我被告知应该可以工作的代码,但它不是。此外,如果已设置警报,则警报对话框应提供取消警报的选项,但这也不起作用。这是我的代码:
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();
}