1. Are you ready for the Galaxy S20? Here is everything we know so far!

Disable alarms with AlarmManager

Discussion in 'Android Development' started by fabiobh, Aug 20, 2012.

  1. fabiobh

    fabiobh Lurker
    Thread Starter

    Hi folks, I'm creating a code that generates an alarm that rings several times in a given time interval.

    [java]
    buttonStart.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View arg0) {
    Intent myIntent = new Intent(Alarm2Activity.this,
    MyAlarmService.class);
    pendingIntent = PendingIntent.getService(Alarm2Activity.this,
    0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 5000,
    pendingIntent);
    Toast.makeText(Alarm2Activity.this, "Start Alarm",
    Toast.LENGTH_LONG).show();
    }
    });
    [/java]
    And use the code below to disable the alarm:
    [java]
    buttonCancel.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View arg0) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    Toast.makeText(Alarm2Activity.this, "Cancel!",
    Toast.LENGTH_LONG).show();
    }
    });
    [/java]
    But when I start the alarm and leave the application, the alarm continues, so far so good, but when I go back into the application and try to disable the alarm by clicking the cancel button and it does not disable.

    The alarm is still running, it only disable, when I start again, and then click in the cancel button.

    My problem here is that once you exit the application and come back, I'm not able to retrieve the alarm, I can create another just over and then cancel.

    How can I fix this?
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Disable alarms AlarmManager
  1. indieben
    Replies:
    2
    Views:
    308
  2. edent
    Replies:
    2
    Views:
    415
  3. justamel
    Replies:
    3
    Views:
    499
  4. probelogic
    Replies:
    3
    Views:
    700
  5. Viridel
    Replies:
    8
    Views:
    705
  6. jansmigura
    Replies:
    2
    Views:
    1,114
  7. kite
    Replies:
    6
    Views:
    1,278
  8. sinthome
    Replies:
    2
    Views:
    678
  9. Oneechan69
    Replies:
    9
    Views:
    1,899
  10. Sunny Rio
    Replies:
    20
    Views:
    5,469

Share This Page

Loading...