1 /*
2 * Copyright 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.repeatingalarm;
18 
19 import android.app.AlarmManager;
20 import android.app.PendingIntent;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.SystemClock;
24 import android.support.v4.app.Fragment;
25 import android.view.MenuItem;
26 import com.example.android.common.logger.*;
27 
28 
29 public class RepeatingAlarmFragment extends Fragment {
30 
31     // This value is defined and consumed by app code, so any value will work.
32     // There's no significance to this sample using 0.
33     public static final int REQUEST_CODE = 0;
34 
35     @Override
onCreate(Bundle savedInstanceState)36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         setHasOptionsMenu(true);
39     }
40 
41     @Override
onOptionsItemSelected(MenuItem item)42     public boolean onOptionsItemSelected(MenuItem item) {
43         if(item.getItemId() == R.id.sample_action) {
44 
45             // BEGIN_INCLUDE (intent_fired_by_alarm)
46             // First create an intent for the alarm to activate.
47             // This code simply starts an Activity, or brings it to the front if it has already
48             // been created.
49             Intent intent = new Intent(getActivity(), MainActivity.class);
50             intent.setAction(Intent.ACTION_MAIN);
51             intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
52             // END_INCLUDE (intent_fired_by_alarm)
53 
54             // BEGIN_INCLUDE (pending_intent_for_alarm)
55             // Because the intent must be fired by a system service from outside the application,
56             // it's necessary to wrap it in a PendingIntent.  Providing a different process with
57             // a PendingIntent gives that other process permission to fire the intent that this
58             // application has created.
59             // Also, this code creates a PendingIntent to start an Activity.  To create a
60             // BroadcastIntent instead, simply call getBroadcast instead of getIntent.
61             PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), REQUEST_CODE,
62                     intent, 0);
63 
64             // END_INCLUDE (pending_intent_for_alarm)
65 
66             // BEGIN_INCLUDE (configure_alarm_manager)
67             // There are two clock types for alarms, ELAPSED_REALTIME and RTC.
68             // ELAPSED_REALTIME uses time since system boot as a reference, and RTC uses UTC (wall
69             // clock) time.  This means ELAPSED_REALTIME is suited to setting an alarm according to
70             // passage of time (every 15 seconds, 15 minutes, etc), since it isn't affected by
71             // timezone/locale.  RTC is better suited for alarms that should be dependant on current
72             // locale.
73 
74             // Both types have a WAKEUP version, which says to wake up the device if the screen is
75             // off.  This is useful for situations such as alarm clocks.  Abuse of this flag is an
76             // efficient way to skyrocket the uninstall rate of an application, so use with care.
77             // For most situations, ELAPSED_REALTIME will suffice.
78             int alarmType = AlarmManager.ELAPSED_REALTIME;
79             final int FIFTEEN_SEC_MILLIS = 15000;
80 
81             // The AlarmManager, like most system services, isn't created by application code, but
82             // requested from the system.
83             AlarmManager alarmManager = (AlarmManager)
84                     getActivity().getSystemService(getActivity().ALARM_SERVICE);
85 
86             // setRepeating takes a start delay and period between alarms as arguments.
87             // The below code fires after 15 seconds, and repeats every 15 seconds.  This is very
88             // useful for demonstration purposes, but horrendous for production.  Don't be that dev.
89             alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + FIFTEEN_SEC_MILLIS,
90                     FIFTEEN_SEC_MILLIS, pendingIntent);
91             // END_INCLUDE (configure_alarm_manager);
92             Log.i("RepeatingAlarmFragment", "Alarm set.");
93         }
94         return true;
95     }
96 }
97