Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import com.example.android.apis.R;
     22 
     23 import android.app.Activity;
     24 import android.app.AlarmManager;
     25 import android.app.PendingIntent;
     26 import android.content.Intent;
     27 import android.os.SystemClock;
     28 import android.os.Bundle;
     29 import android.view.View;
     30 import android.view.View.OnClickListener;
     31 import android.widget.Button;
     32 import android.widget.Toast;
     33 
     34 import java.util.Calendar;
     35 
     36 /**
     37  * Example of scheduling one-shot and repeating alarms.  See
     38  * {@link OneShotAlarm} for the code run when the one-shot alarm goes off, and
     39  * {@link RepeatingAlarm} for the code run when the repeating alarm goes off.
     40  * <h4>Demo</h4>
     41 App/Service/Alarm Controller
     42 
     43 <h4>Source files</h4>
     44 <table class="LinkTable">
     45         <tr>
     46             <td class="LinkColumn">src/com.example.android.apis/app/AlarmController.java</td>
     47             <td class="DescrColumn">The activity that lets you schedule alarms</td>
     48         </tr>
     49         <tr>
     50             <td class="LinkColumn">src/com.example.android.apis/app/OneShotAlarm.java</td>
     51             <td class="DescrColumn">This is an intent receiver that executes when the
     52                 one-shot alarm goes off</td>
     53         </tr>
     54         <tr>
     55             <td class="LinkColumn">src/com.example.android.apis/app/RepeatingAlarm.java</td>
     56             <td class="DescrColumn">This is an intent receiver that executes when the
     57                 repeating alarm goes off</td>
     58         </tr>
     59         <tr>
     60             <td class="LinkColumn">/res/any/layout/alarm_controller.xml</td>
     61             <td class="DescrColumn">Defines contents of the screen</td>
     62         </tr>
     63 </table>
     64 
     65  */
     66 public class AlarmController extends Activity {
     67     Toast mToast;
     68 
     69     @Override
     70 	protected void onCreate(Bundle savedInstanceState) {
     71         super.onCreate(savedInstanceState);
     72 
     73         setContentView(R.layout.alarm_controller);
     74 
     75         // Watch for button clicks.
     76         Button button = (Button)findViewById(R.id.one_shot);
     77         button.setOnClickListener(mOneShotListener);
     78         button = (Button)findViewById(R.id.start_repeating);
     79         button.setOnClickListener(mStartRepeatingListener);
     80         button = (Button)findViewById(R.id.stop_repeating);
     81         button.setOnClickListener(mStopRepeatingListener);
     82     }
     83 
     84     private OnClickListener mOneShotListener = new OnClickListener() {
     85         public void onClick(View v) {
     86             // When the alarm goes off, we want to broadcast an Intent to our
     87             // BroadcastReceiver.  Here we make an Intent with an explicit class
     88             // name to have our own receiver (which has been published in
     89             // AndroidManifest.xml) instantiated and called, and then create an
     90             // IntentSender to have the intent executed as a broadcast.
     91             Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
     92             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
     93                     0, intent, 0);
     94 
     95             // We want the alarm to go off 30 seconds from now.
     96             Calendar calendar = Calendar.getInstance();
     97             calendar.setTimeInMillis(System.currentTimeMillis());
     98             calendar.add(Calendar.SECOND, 30);
     99 
    100             // Schedule the alarm!
    101             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    102             am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    103 
    104             // Tell the user about what we did.
    105             if (mToast != null) {
    106                 mToast.cancel();
    107             }
    108             mToast = Toast.makeText(AlarmController.this, R.string.one_shot_scheduled,
    109                     Toast.LENGTH_LONG);
    110             mToast.show();
    111         }
    112     };
    113 
    114     private OnClickListener mStartRepeatingListener = new OnClickListener() {
    115         public void onClick(View v) {
    116             // When the alarm goes off, we want to broadcast an Intent to our
    117             // BroadcastReceiver.  Here we make an Intent with an explicit class
    118             // name to have our own receiver (which has been published in
    119             // AndroidManifest.xml) instantiated and called, and then create an
    120             // IntentSender to have the intent executed as a broadcast.
    121             // Note that unlike above, this IntentSender is configured to
    122             // allow itself to be sent multiple times.
    123             Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
    124             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
    125                     0, intent, 0);
    126 
    127             // We want the alarm to go off 30 seconds from now.
    128             long firstTime = SystemClock.elapsedRealtime();
    129             firstTime += 15*1000;
    130 
    131             // Schedule the alarm!
    132             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    133             am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    134                             firstTime, 15*1000, sender);
    135 
    136             // Tell the user about what we did.
    137             if (mToast != null) {
    138                 mToast.cancel();
    139             }
    140             mToast = Toast.makeText(AlarmController.this, R.string.repeating_scheduled,
    141                     Toast.LENGTH_LONG);
    142             mToast.show();
    143         }
    144     };
    145 
    146     private OnClickListener mStopRepeatingListener = new OnClickListener() {
    147         public void onClick(View v) {
    148             // Create the same intent, and thus a matching IntentSender, for
    149             // the one that was scheduled.
    150             Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
    151             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
    152                     0, intent, 0);
    153 
    154             // And cancel the alarm.
    155             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    156             am.cancel(sender);
    157 
    158             // Tell the user about what we did.
    159             if (mToast != null) {
    160                 mToast.cancel();
    161             }
    162             mToast = Toast.makeText(AlarmController.this, R.string.repeating_unscheduled,
    163                     Toast.LENGTH_LONG);
    164             mToast.show();
    165         }
    166     };
    167 }
    168 
    169