androidringtonemanager

Alarm keeps ringing even if i press the stop button


I made an alarm clock, it works almost correctly except that it never stops. Even i press my stop the alarm button it keeps ringing.

I have two buttons, one with id setthealarm and another with id stopthealarm.

In public class MainActivity I define TimePicker, PendingIntent and AlarmManager

public class activity_mainnew extends Activity {
TimePicker alarmTimePicker;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;



@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainnew);
alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}

setthetime onClick:

@RequiresApi(api = Build.VERSION_CODES.M)
    public void setthealarm(View view){
        Toast.makeText(activity_mainnew.this, "ALARM ON", Toast.LENGTH_SHORT).show();
        long time;

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
        calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());

        Intent intent = new Intent(this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        alarmTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

        time = (calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
        if(System.currentTimeMillis()>time)
        {
            if (calendar.AM_PM == 0)
                time = time + (1000*60*60*12);
            else
                time = time + (1000*60*60*24);
        }
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
    }

And stopthealarm onClick:

@RequiresApi(api = Build.VERSION_CODES.M)
public void stopthealarm(View view){
    Toast.makeText(activity_mainnew.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    pendingIntent.cancel();
}

in my AlarmReceiver class i have the following:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }

        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();
    }
}

Any idea please why ringtone doesn't stop?


Solution

  • To properly cancel your alarm you need to have your PendingIntent use the FLAG_UPDATE_CURRENT flag.

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    Also add a request code line in your AlarmReceiver class at the beginning, before the onReceive():

    public static final int REQUEST_CODE = 222;
    

    To stop the ringing call stop() on the ringtone when the stop button is clicked:

    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.stop();