user2536314
user2536314

Reputation: 1

Cancelling Alarm Manager and Checking if Alarm Has been set

I have an alarm manager where I want to see if the alarm has been set and if it has display a different alert dialog as apposed ti if it hasn't been set. I have found some code that I was told should work but it isnt. And also if the alarm has been set that Alert Dialog box should give the option to cancel the alarm but that also isn't working. Here is my code:

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();
        }

Upvotes: 0

Views: 5318

Answers (1)

David Wasser
David Wasser

Reputation: 95578

In order to determine whether the alarm is set or not, you are doing this:

boolean alarmUp = (PendingIntent.getBroadcast(Main.this, 1, 
            new Intent(Main.this, Alarm_Receiver.class), 
            PendingIntent.FLAG_NO_CREATE) != null);

But when you set the alarm you do this:

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);

These Intents don't match. You'll need to set the ACTION on the Intent you are using to determine if the alarm is set. Like this:

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);

Upvotes: 7

Related Questions