AlarmCreateActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AlarmCreateActivity extends Activity { private AlarmManager mAlarmManager; private Intent mNotificationReceiverIntent, mLoggerReceiverIntent; private PendingIntent mNotificationReceiverPendingIntent, mLoggerReceiverPendingIntent; private static final long INITIAL_ALARM_DELAY = 2 * 60 * 1000L; protected static final long JITTER = 5000L; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get the AlarmManager Service mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); // Create an Intent to broadcast to the AlarmNotificationReceiver mNotificationReceiverIntent = new Intent(AlarmCreateActivity.this, AlarmNotificationReceiver.class); // Create an PendingIntent that holds the NotificationReceiverIntent mNotificationReceiverPendingIntent = PendingIntent.getBroadcast( AlarmCreateActivity.this, 0, mNotificationReceiverIntent, 0); // Create an Intent to broadcast to the AlarmLoggerReceiver mLoggerReceiverIntent = new Intent(AlarmCreateActivity.this, AlarmLoggerReceiver.class); // Create PendingIntent that holds the mLoggerReceiverPendingIntent mLoggerReceiverPendingIntent = PendingIntent.getBroadcast( AlarmCreateActivity.this, 0, mLoggerReceiverIntent, 0); // Set up single alarm Button final Button singleAlarmButton = (Button) findViewById(R.id.single_alarm_button); singleAlarmButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Set single alarm mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + INITIAL_ALARM_DELAY, mNotificationReceiverPendingIntent); // Set single alarm to fire shortly after previous alarm mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + INITIAL_ALARM_DELAY + JITTER, mLoggerReceiverPendingIntent); // Show Toast message Toast.makeText(getApplicationContext(), "Single Alarm Set", Toast.LENGTH_LONG).show(); } }); // Set up repeating Alarm Button final Button repeatingAlarmButton = (Button) findViewById(R.id.repeating_alarm_button); repeatingAlarmButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Set repeating alarm mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY, AlarmManager.INTERVAL_FIFTEEN_MINUTES, mNotificationReceiverPendingIntent); // Set repeating alarm to fire shortly after previous alarm mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY + JITTER, AlarmManager.INTERVAL_FIFTEEN_MINUTES, mLoggerReceiverPendingIntent); // Show Toast message Toast.makeText(getApplicationContext(), "Repeating Alarm Set", Toast.LENGTH_LONG).show(); } }); // Set up inexact repeating alarm Button final Button inexactRepeatingAlarmButton = (Button) findViewById(R.id.inexact_repeating_alarm_button); inexactRepeatingAlarmButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Set inexact repeating alarm mAlarmManager.setInexactRepeating( AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY, AlarmManager.INTERVAL_FIFTEEN_MINUTES, mNotificationReceiverPendingIntent); // Set inexact repeating alarm to fire shortly after previous alarm mAlarmManager.setInexactRepeating( AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY + JITTER, AlarmManager.INTERVAL_FIFTEEN_MINUTES, mLoggerReceiverPendingIntent); Toast.makeText(getApplicationContext(), "Inexact Repeating Alarm Set", Toast.LENGTH_LONG) .show(); } }); // Set up cancel repeating alarm Button final Button cancelRepeatingAlarmButton = (Button) findViewById(R.id.cancel_repeating_alarm_button); cancelRepeatingAlarmButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Cancel all alarms using mNotificationReceiverPendingIntent mAlarmManager.cancel(mNotificationReceiverPendingIntent); // Cancel all alarms using mLoggerReceiverPendingIntent mAlarmManager.cancel(mLoggerReceiverPendingIntent); // Show Toast message Toast.makeText(getApplicationContext(), "Repeating Alarms Cancelled", Toast.LENGTH_LONG).show(); } }); } } |
AlarmNotificationReceiver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import java.text.DateFormat; import java.util.Date; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.util.Log; public class AlarmNotificationReceiver extends BroadcastReceiver { // Notification ID to allow for future updates private static final int MY_NOTIFICATION_ID = 1; private static final String TAG = "AlarmNotificationReceiver"; // Notification Text Elements private final CharSequence tickerText = "Are You Playing Angry Birds Again!"; private final CharSequence contentTitle = "A Kind Reminder"; private final CharSequence contentText = "Get back to studying!!"; // Notification Action Elements private Intent mNotificationIntent; private PendingIntent mContentIntent; // Notification Sound and Vibration on Arrival private final Uri soundURI = Uri .parse("android.resource://course.examples.Alarms.AlarmCreate/" + R.raw.alarm_rooster); private final long[] mVibratePattern = { 0, 200, 200, 300 }; @Override public void onReceive(Context context, Intent intent) { // The Intent to be used when the user clicks on the Notification View mNotificationIntent = new Intent(context, AlarmCreateActivity.class); // The PendingIntent that wraps the underlying Intent mContentIntent = PendingIntent.getActivity(context, 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); // Build the Notification Notification.Builder notificationBuilder = new Notification.Builder( context).setTicker(tickerText) .setSmallIcon(android.R.drawable.stat_sys_warning) .setAutoCancel(true).setContentTitle(contentTitle) .setContentText(contentText).setContentIntent(mContentIntent) .setSound(soundURI).setVibrate(mVibratePattern); // Get the NotificationManager NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); // Pass the Notification to the NotificationManager: mNotificationManager.notify(MY_NOTIFICATION_ID, notificationBuilder.build()); // Log occurence of notify() call Log.i(TAG, "Sending notification at:" + DateFormat.getDateTimeInstance().format(new Date())); } } |
AlarmLoggerReceiver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.text.DateFormat; import java.util.Date; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class AlarmLoggerReceiver extends BroadcastReceiver { private static final String TAG = "AlarmLoggerReceiver"; @Override public void onReceive(Context context, Intent intent) { // Log receipt of the Intent with timestamp Log.i(TAG,"Logging alarm at:" + DateFormat.getDateTimeInstance().format(new Date())); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/single_alarm_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:text="@string/set_single_alarm" > </Button> <Button android:id="@+id/repeating_alarm_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/set_repeating_alarm" /> <Button android:id="@+id/inexact_repeating_alarm_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/set_inexact_repeating_alarm" /> <Button android:id="@+id/cancel_repeating_alarm_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/cancel_repeating_alarm" /> </LinearLayout> |