Home | History | Annotate | Download | only in content
      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 
     17 package com.example.android.supportv4.content;
     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.Bundle;
     24 import android.view.View;
     25 import android.widget.Button;
     26 import android.widget.Toast;
     27 
     28 import com.example.android.supportv4.R;
     29 
     30 import java.util.Calendar;
     31 
     32 public 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         @Override
     48         public void onClick(View v) {
     49             // When the alarm goes off, we want to broadcast an Intent to our
     50             // BroadcastReceiver.  Here we make an Intent with an explicit class
     51             // name to have our own receiver (which has been published in
     52             // AndroidManifest.xml) instantiated and called, and then create an
     53             // IntentSender to have the intent executed as a broadcast.
     54             Intent intent = new Intent(SimpleWakefulController.this, SimpleWakefulReceiver.class);
     55             PendingIntent sender = PendingIntent.getBroadcast(SimpleWakefulController.this,
     56                     0, intent, 0);
     57 
     58             // We want the alarm to go off 30 seconds from now.
     59             Calendar calendar = Calendar.getInstance();
     60             calendar.setTimeInMillis(System.currentTimeMillis());
     61             calendar.add(Calendar.SECOND, 30);
     62 
     63             // Schedule the alarm!
     64             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
     65             am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
     66 
     67             // Tell the user about what we did.
     68             if (mToast != null) {
     69                 mToast.cancel();
     70             }
     71             mToast = Toast.makeText(SimpleWakefulController.this, R.string.simple_wakeful_scheduled,
     72                     Toast.LENGTH_LONG);
     73             mToast.show();
     74         }
     75     };
     76 }
     77