Example of scheduling one-shot and repeating alarms. : Alarm « Core Class « Android






Example of scheduling one-shot and repeating alarms.

    
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package app.test;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * 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. <h4>Demo</h4>
 * App/Service/Alarm Controller
 * 
 * <h4>Source files</h4>
 * <table class="LinkTable">
 * <tr>
 * <td class="LinkColumn">src/com.example.android.apis/app/Test.java</td>
 * <td class="DescrColumn">The activity that lets you schedule alarms</td>
 * </tr>
 * <tr>
 * <td class="LinkColumn">src/com.example.android.apis/app/OneShotAlarm.java</td>
 * <td class="DescrColumn">This is an intent receiver that executes when the
 * one-shot alarm goes off</td>
 * </tr>
 * <tr>
 * <td class="LinkColumn">src/com.example.android.apis/app/RepeatingAlarm.java</td>
 * <td class="DescrColumn">This is an intent receiver that executes when the
 * repeating alarm goes off</td>
 * </tr>
 * <tr>
 * <td class="LinkColumn">/res/any/layout/alarm_controller.xml</td>
 * <td class="DescrColumn">Defines contents of the screen</td>
 * </tr>
 * </table>
 */
public class Test extends Activity {
  Toast mToast;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // Watch for button clicks.
    Button button = (Button) findViewById(R.id.one_shot);
    button.setOnClickListener(mOneShotListener);
    button = (Button) findViewById(R.id.start_repeating);
    button.setOnClickListener(mStartRepeatingListener);
    button = (Button) findViewById(R.id.stop_repeating);
    button.setOnClickListener(mStopRepeatingListener);
  }

  private OnClickListener mOneShotListener = new OnClickListener() {
    public void onClick(View v) {
      // When the alarm goes off, we want to broadcast an Intent to our
      // BroadcastReceiver. Here we make an Intent with an explicit class
      // name to have our own receiver (which has been published in
      // AndroidManifest.xml) instantiated and called, and then create an
      // IntentSender to have the intent executed as a broadcast.
      Intent intent = new Intent(Test.this, OneShotAlarm.class);
      PendingIntent sender = PendingIntent.getBroadcast(Test.this, 0,
          intent, 0);

      // We want the alarm to go off 30 seconds from now.
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(System.currentTimeMillis());
      calendar.add(Calendar.SECOND, 30);

      // Schedule the alarm!
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

      // Tell the user about what we did.
      if (mToast != null) {
        mToast.cancel();
      }
      mToast = Toast.makeText(Test.this, "one_shot_scheduled",
          Toast.LENGTH_LONG);
      mToast.show();
    }
  };

  private OnClickListener mStartRepeatingListener = new OnClickListener() {
    public void onClick(View v) {
      // When the alarm goes off, we want to broadcast an Intent to our
      // BroadcastReceiver. Here we make an Intent with an explicit class
      // name to have our own receiver (which has been published in
      // AndroidManifest.xml) instantiated and called, and then create an
      // IntentSender to have the intent executed as a broadcast.
      // Note that unlike above, this IntentSender is configured to
      // allow itself to be sent multiple times.
      Intent intent = new Intent(Test.this, RepeatingAlarm.class);
      PendingIntent sender = PendingIntent.getBroadcast(Test.this, 0,
          intent, 0);

      // We want the alarm to go off 30 seconds from now.
      long firstTime = SystemClock.elapsedRealtime();
      firstTime += 15 * 1000;

      // Schedule the alarm!
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
          15 * 1000, sender);

      // Tell the user about what we did.
      if (mToast != null) {
        mToast.cancel();
      }
      mToast = Toast.makeText(Test.this, "repeating_scheduled",
          Toast.LENGTH_LONG);
      mToast.show();
    }
  };

  private OnClickListener mStopRepeatingListener = new OnClickListener() {
    public void onClick(View v) {
      // Create the same intent, and thus a matching IntentSender, for
      // the one that was scheduled.
      Intent intent = new Intent(Test.this, RepeatingAlarm.class);
      PendingIntent sender = PendingIntent.getBroadcast(Test.this, 0,
          intent, 0);

      // And cancel the alarm.
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      am.cancel(sender);

      // Tell the user about what we did.
      if (mToast != null) {
        mToast.cancel();
      }
      mToast = Toast.makeText(Test.this, "repeating_unscheduled",
          Toast.LENGTH_LONG);
      mToast.show();
    }
  };
}

/**
 * This is an example of implement an {@link BroadcastReceiver} for an alarm that
 * should occur once.
 * <p>
 * When the alarm goes off, we show a <i>Toast</i>, a quick message.
 */
 class OneShotAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "one_shot_received", Toast.LENGTH_SHORT).show();
    }
}


/**
 * This is an example of implement an {@link BroadcastReceiver} for an alarm
 * that should occur once.
 */
class RepeatingAlarm extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "repeating_received", Toast.LENGTH_SHORT)
        .show();
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Demonstrates starting and stopping a local service.
     See corresponding Java code com.android.sdk.app.LocalSerice.java. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView 
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="alarm_controller"/>

    <Button android:id="@+id/one_shot"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="one_shot_alarm">
        <requestFocus />
    </Button>

    <Button android:id="@+id/start_repeating"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="start_repeating_alarm" />

    <Button android:id="@+id/stop_repeating"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="stop_repeating_alarm" />

</LinearLayout>

   
    
    
    
  








Related examples in the same category

1.Alarm system
2.Alarm demo
3.Alarm Activity