1

I am having trouble to stop the alarm from ringing even after I have pressed the stopAlarm button. thankyou

This is my start alarm switch in MainActivity java class.

public void switchClicked (View view) {
        if (((Switch) view).isChecked()) {
            Log.d("MainActivity", "Alarm On");
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());
            Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
            alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
            setAlarmText("ON");
        } else {
            alarmManager.cancel(pendingIntent);
            setAlarmText("OFF");
            Log.d("MainActivity", "Alarm Off");
        }
    }
    public void setAlarmText(String alarmText) {
        alarmTextView.setText(alarmText);
    }

Here is my StopAlarm button in MainActivity java class.

public void stopAlarm(View view) {
        setAlarmText("Alarm stopped");
        Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
        alarmManager.cancel(pendingIntent);



            }

This is the AlarmReciver java class.

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {

        MainActivity inst = MainActivity.instance();
        inst.setAlarmText("Alarm! Wake up! Wake up!");
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();
    }
}
laalto
  • 150,114
  • 66
  • 286
  • 303
  • Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. – Zoe Apr 03 '19 at 20:09

1 Answers1

0

stopAlarm method should create PendingIntent with PendingIntent.FLAG_UPDATE_CURRENT to overwrite previous Intent, have you tried this?

pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thank you for the help, but it did not seem to work. The alarm still kept ringing after I pressed the Stop button. Do you know any other way that would make the button work? Thank you for any help snachmsm. –  Mar 07 '19 at 13:02
  • 1
    you are starting alarm with `PendingIntent` created by `PendingIntent.getBroadcast`, but you are trying to cancel it with `PendingIntent.getService`. In both places it should be `getBroadcast` (as `AlarmReceiver extends BroadcastReceiver`) – snachmsm Mar 07 '19 at 19:33
  • Again, thank you so much for the help but I just tried what you have told me but the stop button still not working. Do you know any other way that would make the button work? Thank you for any help snachmsm. –  Mar 07 '19 at 19:53
  • 1
    Ok, now I see what going on here... You are manually calling `ringotne.play();` but never stopping it... your method `stopAlarm(View view)` in fact is cancelling potential pending previously set alarm. If it already fired then `stopAlarm` does exacly nothing (besides that it wasn't working with `getService` and without updating flag, so didn't working at all). What do you want to achieve is to get access to same `Ringtone` instance and call `stop()`, [which is tricky](https://stackoverflow.com/questions/14089380/how-do-i-stop-the-currently-playing-ringtone) – snachmsm Mar 07 '19 at 21:16
  • 1
    Also I'm warning you that `MainActivity.instance()` pattern is VERY bad and buggy, may cause memory leaks and crashes... – snachmsm Mar 07 '19 at 21:17
  • I am so grateful for your help, this is actually my first ever app and first time coding and it is hard for me to understand all these things. Also, I have been waiting for answers through many reposts of this problem and you are my hero. I will try to get it to work this weekend because I am a student and have other work to do. May I please contact you if I have encountered any problem in the future? Thank you so much for your time and help. –  Mar 07 '19 at 21:30
  • 1
    you may call me in this thread with putting new comment with link to new question and I will try to help. Good luck! :) – snachmsm Mar 08 '19 at 12:36
  • Hello, @snachmsm I have been working on the "get access to same Ringtone instance and call stop()" that you have suggested me to do but I cannot figure out how to do this after reading the post that you have linked for me. Please, can you give more guidance on how this can be achieved? This is the new thread link for this problem https://stackoverflow.com/questions/55077221/cannot-figure-out-how-to-create-the-cancel-button-for-ringing-alarm . Thankyou so much, this will help me a lot, I don't want to take up much of your time so if you're unable to help me, I can understand. –  Mar 09 '19 at 12:21