SimpleWakefulController.java revision 892f76ff05e47ede28413dcdc4cb8036dee8bece
1/* 2 * Copyright (C) 2013 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 17package com.example.android.supportv4.content; 18 19import com.example.android.supportv4.R; 20 21import android.app.Activity; 22import android.app.AlarmManager; 23import android.app.PendingIntent; 24import android.content.Intent; 25import android.os.Bundle; 26import android.view.View; 27import android.widget.Button; 28import android.widget.Toast; 29 30import java.util.Calendar; 31 32public class SimpleWakefulController extends Activity { 33 Toast mToast; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 39 setContentView(R.layout.wakeful_alarm_controller); 40 41 // Watch for button clicks. 42 Button button = (Button)findViewById(R.id.schedule); 43 button.setOnClickListener(mScheduleListener); 44 } 45 46 private View.OnClickListener mScheduleListener = new View.OnClickListener() { 47 public void onClick(View v) { 48 // When the alarm goes off, we want to broadcast an Intent to our 49 // BroadcastReceiver. Here we make an Intent with an explicit class 50 // name to have our own receiver (which has been published in 51 // AndroidManifest.xml) instantiated and called, and then create an 52 // IntentSender to have the intent executed as a broadcast. 53 Intent intent = new Intent(SimpleWakefulController.this, SimpleWakefulReceiver.class); 54 PendingIntent sender = PendingIntent.getBroadcast(SimpleWakefulController.this, 55 0, intent, 0); 56 57 // We want the alarm to go off 30 seconds from now. 58 Calendar calendar = Calendar.getInstance(); 59 calendar.setTimeInMillis(System.currentTimeMillis()); 60 calendar.add(Calendar.SECOND, 30); 61 62 // Schedule the alarm! 63 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 64 am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 65 66 // Tell the user about what we did. 67 if (mToast != null) { 68 mToast.cancel(); 69 } 70 mToast = Toast.makeText(SimpleWakefulController.this, R.string.simple_wakeful_scheduled, 71 Toast.LENGTH_LONG); 72 mToast.show(); 73 } 74 }; 75} 76