• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.newalarm;
18 
19 import android.app.Activity;
20 import android.app.AlarmManager;
21 import android.app.PendingIntent;
22 import android.content.Intent;
23 import android.os.SystemClock;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.widget.Button;
28 import android.widget.Toast;
29 
30 /**
31  * This is the activity that controls AlarmService.
32  * <p>
33  * When the user clicks the "Start Alarm Service" button, it triggers a repeating countdown
34  * timer. Every thirty seconds, the timer starts AlarmService, which then runs for 15 seconds
35  * and shuts itself down.
36  * </p>
37  * <p>
38  * When the user clicks the "Stop Alarm Service" button, it stops the countdown timer.
39  * </p>
40  */
41 
42 public class AlarmActivity extends Activity {
43     // 30 seconds in milliseconds
44     private static final long THIRTY_SECONDS_MILLIS = 30 * 1000;
45 
46     // An intent for AlarmService, to trigger it as if the Activity called startService().
47     private PendingIntent mAlarmSender;
48 
49     // Contains a handle to the system alarm service
50     private AlarmManager mAlarmManager;
51 
52     /**
53      * This method is called when Android starts the activity. It initializes the UI.
54      * <p>
55      * This method is automatically called when Android starts the Activity
56      * </p>
57      */
58     @Override
onCreate(Bundle savedInstanceState)59     protected void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61 
62         // Create a PendingIntent to trigger a startService() for AlarmService
63         mAlarmSender = PendingIntent.getService(  // set up an intent for a call to a service
64             AlarmActivity.this,  // the current context
65             0,  // request code (not used)
66             new Intent(AlarmActivity.this, AlarmService.class),  // A new Service intent
67             0   // flags (none are required for a service)
68         );
69 
70         // Creates the main view
71         setContentView(R.layout.main);
72 
73         // Finds the button that starts the repeating countdown timer
74         Button button = (Button)findViewById(R.id.start_alarm);
75 
76         // Sets the listener for the start button
77         button.setOnClickListener(mStartAlarmListener);
78 
79         // Finds the button that stops countdown timer
80         button = (Button)findViewById(R.id.stop_alarm);
81 
82         // Sets the listener for the stop button
83         button.setOnClickListener(mStopAlarmListener);
84 
85         // Gets the handle to the system alarm service
86         mAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
87     }
88 
89     // Creates a new anonymous click listener for the start button. It starts the repeating
90     //  countdown timer.
91     private OnClickListener mStartAlarmListener = new OnClickListener() {
92         // Sets the callback for when the button is clicked
93         public void onClick(View v) {
94 
95             // Sets the time when the alarm will first go off
96             // The Android AlarmManager uses this form of the current time.
97             long firstAlarmTime = SystemClock.elapsedRealtime();
98 
99             // Sets a repeating countdown timer that triggers AlarmService
100             mAlarmManager.setRepeating(
101                 AlarmManager.ELAPSED_REALTIME_WAKEUP, // based on time since last wake up
102                 firstAlarmTime,  // sends the first alarm immediately
103                 THIRTY_SECONDS_MILLIS,  // repeats every thirty seconds
104                 mAlarmSender  // when the alarm goes off, sends this Intent
105             );
106 
107             // Notifies the user that the repeating countdown timer has been started
108             Toast.makeText(
109                 AlarmActivity.this,  //  the current context
110                 R.string.repeating_started,  // the message to display
111                 Toast.LENGTH_LONG  // how long to display the message
112             ).show();  // show the message on the screen
113         }
114     };
115 
116     // Creates a new anonymous click listener for the stop button. It shuts off the repeating
117     // countdown timer.
118     private OnClickListener mStopAlarmListener = new OnClickListener() {
119         // Sets the callback for when the button is clicked
120         public void onClick(View v) {
121 
122             // Cancels the repeating countdown timer
123             mAlarmManager.cancel(mAlarmSender);
124 
125             // Notifies the user that the repeating countdown timer has been stopped
126             Toast.makeText(
127                 AlarmActivity.this,  //  the current context
128                 R.string.repeating_stopped,  // the message to display
129                 Toast.LENGTH_LONG  // how long to display the message
130             ).show(); // display the message
131         }
132     };
133 }
134