Thursday, May 24, 2012

Cancel alarm with a matching PendingIntent

Call cancel(PendingIntent) method of AlarmManager to remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.

Cancel alarm with a matching PendingIntent


Work on the former exercise "Create alarm set on a specified time, using AlarmManager and BroadcastReceiver." Modify AndroidTimeActivity.java, the code to cancel alarm is in cancelAlarm().
package com.exercise.AndroidTime;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class AndroidTimeActivity extends Activity {
 
 TimePicker myTimePicker;
 Button buttonstartSetDialog;
 Button buttonCancelAlarm;
 TextView textAlarmPrompt;
 
 TimePickerDialog timePickerDialog;
 
 final static int RQS_1 = 1;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);
        
        buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
        buttonstartSetDialog.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    textAlarmPrompt.setText("");
    openTimePickerDialog(false);
    
   }});

        buttonCancelAlarm = (Button)findViewById(R.id.cancel);
        buttonCancelAlarm.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    cancelAlarm();
   }});
        
    }

 private void openTimePickerDialog(boolean is24r){
  Calendar calendar = Calendar.getInstance();
  
  timePickerDialog = new TimePickerDialog(
    AndroidTimeActivity.this, 
    onTimeSetListener, 
    calendar.get(Calendar.HOUR_OF_DAY), 
    calendar.get(Calendar.MINUTE), 
    is24r);
  timePickerDialog.setTitle("Set Alarm Time");  
        
  timePickerDialog.show();

 }
    
    OnTimeSetListener onTimeSetListener
    = new OnTimeSetListener(){

  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

   Calendar calNow = Calendar.getInstance();
   Calendar calSet = (Calendar) calNow.clone();

   calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
   calSet.set(Calendar.MINUTE, minute);
   calSet.set(Calendar.SECOND, 0);
   calSet.set(Calendar.MILLISECOND, 0);
   
   if(calSet.compareTo(calNow) <= 0){
    //Today Set time passed, count to tomorrow
    calSet.add(Calendar.DATE, 1);
   }
   
   setAlarm(calSet);
  }};

 private void setAlarm(Calendar targetCal){

  textAlarmPrompt.setText(
    "\n\n***\n"
    + "Alarm is set@ " + targetCal.getTime() + "\n"
    + "***\n");
  
  Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
  AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

 }
 
 private void cancelAlarm(){

  textAlarmPrompt.setText(
    "\n\n***\n"
    + "Alarm Cancelled! \n"
    + "***\n");
  
  Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
  AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  alarmManager.cancel(pendingIntent);

 }
  
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/startSetDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Set Target Time"/>
    <Button
        android:id="@+id/cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Cancel Alarm"/>
    <TextView
        android:id="@+id/alarmprompt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


Both AlarmReceiver.java and AndroidManifest.xml keep no change, refer to the exercise "Create alarm set on a specified time, using AlarmManager and BroadcastReceiver."

Download the files.

Next:
- schedule a repeating alarm


3 comments:

Rojy George said...

how to make it repeating every day

Anonymous said...

como se puede crear varias alarmas???

Howard Oi said...

how do i close the alarm in another activity?