FileDocCategorySizeDatePackage
AlarmService.javaAPI DocGoogle Android v1.5 Example5385Sun Nov 11 13:01:04 GMT 2007com.google.android.samples.app

AlarmService

public class AlarmService extends android.app.Activity

Example of scheduling one-shot and repeating alarms. See {@link OneShotAlarm} for the code run when the one-shot alarm goes off, and {@link RepeatingAlarm} for the code run when the repeating alarm goes off. This demonstrates a very common background that puts together both alarms and service: a regularily scheduled alarm that results in the execution of a relatively long-lived service. An example of where you would use this is for background retrieval of mail. In this situation, you don't want to retrieve the mail directly in the alarm's intent receiver, because this would block others while you are working. Instead, the alarm starts a service that takes care of retrieving the mail.

Demo

App/Service/Alarm Service

Source files

src/com/google/android/samples/app/AlarmService.java The activity that lets you schedule the alarm
src/com/google/android/samples/app/AlarmService_Alarm.java This is an intent receiver that executes when the alarm goes off
src/com/google/android/samples/app/AlarmService_Service.java This is the service that implements our background action, which is started by the AlarmService_Alarm
/res/any/layout/alarm_service.xml Defines contents of the screen

Fields Summary
private android.view.View.OnClickListener
mStartAlarmListener
private android.view.View.OnClickListener
mStopAlarmListener
Constructors Summary
Methods Summary
protected voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        setContentView(R.layout.alarm_service);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.start_alarm);
        button.setOnClickListener(mStartAlarmListener);
        button = (Button)findViewById(R.id.stop_alarm);
        button.setOnClickListener(mStopAlarmListener);