Cancel Alarm using alarmManager.cancel(pendingIntent)

In the exercise "Using AlarmManager to start a Scheduled Activity", the alarm will be started and trigger MyScheduledReceiver repeatly until device rebooted. Such that I will add a button in MyScheduledActivity to cancel the alarm.

Cancel Alarm using alarmManager.cancel(pendingIntent)

To cancel alarm, we can use the function alarmManager.cancel(pendingIntent).

But...When the MyScheduledActivity is started by MyScheduledReceiver, the main activity AndroidScheduledActivity may be already killed by system or by Task Killer; so we cannot call any function in AndroidScheduledActivity to retrieve the AlarmManager and PendingIntent to cancel the alarm. So we have to re-create both AlarmManager and PendingIntent in MyScheduledActivity.

Modify layout_scheduledactivity.xml to add a Stop button for user to cancel the alarm.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="--- Scheduled Activity ---"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Stop! "
/>
<Button
android:id="@+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Dismiss"
/>
</LinearLayout>


Modify MyScheduledActivity to implement buttonStop.setOnClickListener() and override onClick() to retrieve AlarmManager and PendingIntent, and cancel the alarm using alarmManager.cancel(pendingIntent).
package com.exercise.AndroidScheduledActivity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyScheduledActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scheduledactivity);

Button buttonDismiss = (Button)findViewById(R.id.dismiss);
Button buttonStop = (Button)findViewById(R.id.stop);

buttonDismiss.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}});

buttonStop.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(getBaseContext(),
MyScheduledReceiver.class);

PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);

AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);

alarmManager.cancel(pendingIntent);

Intent intent = new Intent();
intent.setClass(MyScheduledActivity.this,
AndroidScheduledActivity.class);
startActivity(intent);
finish();
}});
}

}


Remark: I don't know if it's the formal method to cancel alarm without knowing the original AlarmManager and PendingIntent! But it work as expected in my test.

Download the files.