• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 /**
36  * This demonstrates how you can schedule an alarm that causes a service to
37  * be started.  This is useful when you want to schedule alarms that initiate
38  * long-running operations, such as retrieving recent e-mails.
39  */
40 public class AlarmService extends Activity {
41     private PendingIntent mAlarmSender;
42 
43     @Override
onCreate(Bundle savedInstanceState)44 	protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         // Create an IntentSender that will launch our service, to be scheduled
48         // with the alarm manager.
49         mAlarmSender = PendingIntent.getService(AlarmService.this,
50                 0, new Intent(AlarmService.this, AlarmService_Service.class), 0);
51 
52         setContentView(R.layout.alarm_service);
53 
54         // Watch for button clicks.
55         Button button = (Button)findViewById(R.id.start_alarm);
56         button.setOnClickListener(mStartAlarmListener);
57         button = (Button)findViewById(R.id.stop_alarm);
58         button.setOnClickListener(mStopAlarmListener);
59     }
60 
61     private OnClickListener mStartAlarmListener = new OnClickListener() {
62         public void onClick(View v) {
63             // We want the alarm to go off 30 seconds from now.
64             long firstTime = SystemClock.elapsedRealtime();
65 
66             // Schedule the alarm!
67             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
68             am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
69                             firstTime, 30*1000, mAlarmSender);
70 
71             // Tell the user about what we did.
72             Toast.makeText(AlarmService.this, R.string.repeating_scheduled,
73                     Toast.LENGTH_LONG).show();
74         }
75     };
76 
77     private OnClickListener mStopAlarmListener = new OnClickListener() {
78         public void onClick(View v) {
79             // And cancel the alarm.
80             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
81             am.cancel(mAlarmSender);
82 
83             // Tell the user about what we did.
84             Toast.makeText(AlarmService.this, R.string.repeating_unscheduled,
85                     Toast.LENGTH_LONG).show();
86 
87         }
88     };
89 }
90