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 // Start with: 67 // adb shell am start com.example.android.apis/.app.AlarmController 68 public class AlarmController extends Activity { 69 Toast mToast; 70 71 @Override onCreate(Bundle savedInstanceState)72 protected void onCreate(Bundle savedInstanceState) { 73 super.onCreate(savedInstanceState); 74 75 setContentView(R.layout.alarm_controller); 76 77 // Watch for button clicks. 78 Button button = (Button)findViewById(R.id.one_shot); 79 button.setOnClickListener(mOneShotListener); 80 button = (Button)findViewById(R.id.one_shot_while_idle); 81 button.setOnClickListener(mOneShotListener); 82 button = (Button)findViewById(R.id.start_repeating); 83 button.setOnClickListener(mStartRepeatingListener); 84 button = (Button)findViewById(R.id.stop_repeating); 85 button.setOnClickListener(mStopRepeatingListener); 86 } 87 88 private OnClickListener mOneShotListener = new OnClickListener() { 89 public void onClick(View v) { 90 // When the alarm goes off, we want to broadcast an Intent to our 91 // BroadcastReceiver. Here we make an Intent with an explicit class 92 // name to have our own receiver (which has been published in 93 // AndroidManifest.xml) instantiated and called, and then create an 94 // IntentSender to have the intent executed as a broadcast. 95 Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); 96 PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 97 0, intent, 0); 98 99 // We want the alarm to go off 30 seconds from now. 100 Calendar calendar = Calendar.getInstance(); 101 calendar.setTimeInMillis(System.currentTimeMillis()); 102 calendar.add(Calendar.SECOND, 30); 103 104 // Schedule the alarm! 105 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 106 107 switch (v.getId()) { 108 case R.id.one_shot: 109 am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 110 break; 111 default: 112 am.setExactAndAllowWhileIdle( 113 AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 114 break; 115 } 116 117 // Tell the user about what we did. 118 if (mToast != null) { 119 mToast.cancel(); 120 } 121 mToast = Toast.makeText(AlarmController.this, R.string.one_shot_scheduled, 122 Toast.LENGTH_LONG); 123 mToast.show(); 124 } 125 }; 126 127 private OnClickListener mStartRepeatingListener = new OnClickListener() { 128 public void onClick(View v) { 129 // When the alarm goes off, we want to broadcast an Intent to our 130 // BroadcastReceiver. Here we make an Intent with an explicit class 131 // name to have our own receiver (which has been published in 132 // AndroidManifest.xml) instantiated and called, and then create an 133 // IntentSender to have the intent executed as a broadcast. 134 // Note that unlike above, this IntentSender is configured to 135 // allow itself to be sent multiple times. 136 Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); 137 PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 138 0, intent, 0); 139 140 // We want the alarm to go off 30 seconds from now. 141 long firstTime = SystemClock.elapsedRealtime(); 142 firstTime += 15*1000; 143 144 // Schedule the alarm! 145 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 146 am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 147 firstTime, 15*1000, sender); 148 149 // Tell the user about what we did. 150 if (mToast != null) { 151 mToast.cancel(); 152 } 153 mToast = Toast.makeText(AlarmController.this, R.string.repeating_scheduled, 154 Toast.LENGTH_LONG); 155 mToast.show(); 156 } 157 }; 158 159 private OnClickListener mStopRepeatingListener = new OnClickListener() { 160 public void onClick(View v) { 161 // Create the same intent, and thus a matching IntentSender, for 162 // the one that was scheduled. 163 Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); 164 PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, 165 0, intent, 0); 166 167 // And cancel the alarm. 168 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 169 am.cancel(sender); 170 171 // Tell the user about what we did. 172 if (mToast != null) { 173 mToast.cancel(); 174 } 175 mToast = Toast.makeText(AlarmController.this, R.string.repeating_unscheduled, 176 Toast.LENGTH_LONG); 177 mToast.show(); 178 } 179 }; 180 } 181 182