Advertisement
Guest User

Untitled

a guest
Jul 19th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.06 KB | None | 0 0
  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.abc;
  18.  
  19. import android.app.Activity;
  20. import android.app.AlarmManager;
  21. import android.app.PendingIntent;
  22. import android.content.Intent;
  23. import android.content.Context;
  24. import android.content.BroadcastReceiver;
  25. import android.os.SystemClock;
  26. import android.os.Bundle;
  27. import android.view.View;
  28. import android.view.View.OnClickListener;
  29. import android.widget.Button;
  30. import android.widget.Toast;
  31.  
  32. import java.util.Calendar;
  33.  
  34. /**
  35.  * Example of scheduling one-shot and repeating alarms.  See
  36.  * {@link OneShotAlarm} for the code run when the one-shot alarm goes off, and
  37.  * {@link RepeatingAlarm} for the code run when the repeating alarm goes off.
  38.  * <h4>Demo</h4>
  39. App/Service/Alarm Controller
  40.  
  41. <h4>Source files</h4>
  42. <table class="LinkTable">
  43.         <tr>
  44.             <td class="LinkColumn">src/com.example.android.apis/app/AlarmController.java</td>
  45.             <td class="DescrColumn">The activity that lets you schedule alarms</td>
  46.         </tr>
  47.         <tr>
  48.             <td class="LinkColumn">src/com.example.android.apis/app/OneShotAlarm.java</td>
  49.             <td class="DescrColumn">This is an intent receiver that executes when the
  50.                 one-shot alarm goes off</td>
  51.         </tr>
  52.         <tr>
  53.             <td class="LinkColumn">src/com.example.android.apis/app/RepeatingAlarm.java</td>
  54.             <td class="DescrColumn">This is an intent receiver that executes when the
  55.                 repeating alarm goes off</td>
  56.         </tr>
  57.         <tr>
  58.             <td class="LinkColumn">/res/any/layout/alarm_controller.xml</td>
  59.             <td class="DescrColumn">Defines contents of the screen</td>
  60.         </tr>
  61. </table>
  62.  
  63.  */
  64. public class Abc extends Activity {
  65.     Toast mToast;
  66.  
  67.     @Override
  68.     // Syntax error on token "Bundle", @ expected
  69.     // Syntax error, insert "Type VariableDeclaratorID" to complete FormalParameterList
  70.     protected void onCreate(Bundle wifi.setWifiEnabled(false)) {
  71.         super.onCreate(wifi.setWifiEnabled(false));
  72.  
  73.         setContentView(R.layout.alarm_controller);
  74.  
  75.         // Watch for button clicks.
  76.         Button button = (Button)findViewById(R.id.one_shot);
  77.         button.setOnClickListener(mOneShotListener);
  78.         button = (Button)findViewById(R.id.start_repeating);
  79.         button.setOnClickListener(mStartRepeatingListener);
  80.         button = (Button)findViewById(R.id.stop_repeating);
  81.         button.setOnClickListener(mStopRepeatingListener);
  82.     }
  83.  
  84.     private OnClickListener mOneShotListener = new OnClickListener() {
  85.         public void onClick(View v) {
  86.             // When the alarm goes off, we want to broadcast an Intent to our
  87.             // BroadcastReceiver.  Here we make an Intent with an explicit class
  88.             // name to have our own receiver (which has been published in
  89.             // AndroidManifest.xml) instantiated and called, and then create an
  90.             // IntentSender to have the intent executed as a broadcast.
  91.             Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
  92.             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
  93.                     0, intent, 0);
  94.  
  95.             // We want the alarm to go off 60 seconds from now.
  96.             Calendar calendar = Calendar.getInstance();
  97.             calendar.setTimeInMillis(System.currentTimeMillis());
  98.             calendar.add(Calendar.SECOND, 60);
  99.  
  100.             // Schedule the alarm!
  101.             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  102.             am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
  103.  
  104.         }
  105.     };
  106.  
  107.     private OnClickListener mStartRepeatingListener = new OnClickListener() {
  108.         public void onClick(View v) {
  109.             // When the alarm goes off, we want to broadcast an Intent to our
  110.             // BroadcastReceiver.  Here we make an Intent with an explicit class
  111.             // name to have our own receiver (which has been published in
  112.             // AndroidManifest.xml) instantiated and called, and then create an
  113.             // IntentSender to have the intent executed as a broadcast.
  114.             // Note that unlike above, this IntentSender is configured to
  115.             // allow itself to be sent multiple times.
  116.             Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
  117.             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
  118.                     0, intent, 0);
  119.            
  120.             // We want the alarm to go off 60 seconds from now.
  121.             long firstTime = SystemClock.elapsedRealtime();
  122.             firstTime += 30*1000;
  123.  
  124.             // Schedule the alarm!
  125.             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  126.             am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
  127.                             firstTime, 30*1000, sender);
  128.         }
  129.     };
  130.  
  131.     private OnClickListener mStopRepeatingListener = new OnClickListener() {
  132.         public void onClick(View v) {
  133.             // Create the same intent, and thus a matching IntentSender, for
  134.             // the one that was scheduled.
  135.             Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
  136.             PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
  137.                     0, intent, 0);
  138.            
  139.             // And cancel the alarm.
  140.             AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  141.             am.cancel(sender);
  142.  
  143.         }
  144.     };
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement