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