I've browsed around the internet on how to cancel a repeating alarm with the alarm manager. The only way that I have found is by calling cancel with an identical pending intent to the one used to start the alarm manager. The problem is that instead of cancelling the alarm already deployed, the alarm is unfazed and continues to notify me.
What should happen is that the alarm should be cancelled, then a new alarm should start up. The new alarm does not start though, so the old one is the only alarm that runs.
Just so you know, in the MainActivity class, there is a button that triggers the method startAlarm. That is not an issue.
Here is the important code:
MainActivity.class (only the relevant stuff)
AlarmReciever.class
No errors in the console, it just fails.
What should happen is that the alarm should be cancelled, then a new alarm should start up. The new alarm does not start though, so the old one is the only alarm that runs.
Just so you know, in the MainActivity class, there is a button that triggers the method startAlarm. That is not an issue.
Here is the important code:
MainActivity.class (only the relevant stuff)
Code:
public void startAlarm(View view) {
EditText nameInput = (EditText) this.findViewById(R.id.nameInput);
EditText firstAlertInput = (EditText) this.findViewById(R.id.firstAlertInput);
EditText intervalInput = (EditText) this.findViewById(R.id.intervalInput);
EditText descriptionInput = (EditText) this.findViewById(R.id.descriptionInput);
Intent intent = new Intent(this, AlarmReciever.class);
intent.putExtra("name", nameInput.getText().toString());
intent.putExtra("description", descriptionInput.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int[] times = parseTime(firstAlertInput.getText().toString());
times[0] = times[0] * 3600000;
times[1] = times[1] * 60000;
long millis = times[0] + times[1];
int[] intervalTimes = parseTime(intervalInput.getText().toString());
intervalTimes[0] = intervalTimes[0] * 3600000;
intervalTimes[1] = intervalTimes[1] * 60000;
long intervalMillis = intervalTimes[0] + intervalTimes[1];
cancelCurrentAlarms(pendingIntent, alarmManager);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millis, intervalMillis, pendingIntent);
}
private void cancelCurrentAlarms(PendingIntent pendingIntent, AlarmManager alarmManager){
alarmManager.cancel(pendingIntent);
}
private int[] parseTime(String input){
int[] times = new int[2];
String[] split = input.split(":");
for(int i = 0; i < times.length; i++){
times[i] = Integer.parseInt(split[i]);
}
return times;
}
AlarmReciever.class
Code:
private Context context;
private String name;
private String description;
@Override
public void onReceive(Context c, Intent intent) {
name = "" + intent.getStringExtra("name");
description = "" + intent.getStringExtra("description");
context = c;
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(description)
.setSmallIcon(R.drawable.notificationicon)
.setLights(Color.rgb(78, 155, 222), 1000, 1000)
.setAutoCancel(true)
.setContentTitle(name);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
No errors in the console, it just fails.