Created
March 3, 2014 22:33
-
-
Save csdear/ff0d965da71e9445bba2 to your computer and use it in GitHub Desktop.
Alarms : AlarmManager : Single, Repeating, Inexact Repeating Alarm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //AlarmCreateActivity.java | |
| package course.examples.Alarms.AlarmCreate; | |
| 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); | |
| //1. Get the AlarmManager Service | |
| mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); | |
| //2. Create PendingIntent to start the AlarmNotificationReceiver | |
| mNotificationReceiverIntent = new Intent(AlarmCreateActivity.this, | |
| AlarmNotificationReceiver.class); | |
| mNotificationReceiverPendingIntent = PendingIntent.getBroadcast( | |
| AlarmCreateActivity.this, 0, mNotificationReceiverIntent, 0); | |
| // Create PendingIntent to start the AlarmLoggerReceiver | |
| mLoggerReceiverIntent = new Intent(AlarmCreateActivity.this, | |
| AlarmLoggerReceiver.class); | |
| mLoggerReceiverPendingIntent = PendingIntent.getBroadcast( | |
| AlarmCreateActivity.this, 0, mLoggerReceiverIntent, 0); | |
| // Single Alarm Button | |
| final Button singleAlarmButton = (Button) findViewById(R.id.single_alarm_button); | |
| singleAlarmButton.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| mAlarmManager.set(AlarmManager.RTC_WAKEUP, | |
| System.currentTimeMillis() + INITIAL_ALARM_DELAY, | |
| mNotificationReceiverPendingIntent); | |
| mAlarmManager.set(AlarmManager.RTC_WAKEUP, | |
| System.currentTimeMillis() + INITIAL_ALARM_DELAY | |
| + JITTER, mLoggerReceiverPendingIntent); | |
| Toast.makeText(getApplicationContext(), "Single Alarm Set", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| }); | |
| // Repeating Alarm Button | |
| final Button repeatingAlarmButton = (Button) findViewById(R.id.repeating_alarm_button); | |
| repeatingAlarmButton.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, | |
| SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY, | |
| AlarmManager.INTERVAL_FIFTEEN_MINUTES, | |
| mNotificationReceiverPendingIntent); | |
| mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, | |
| SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY | |
| + JITTER, | |
| AlarmManager.INTERVAL_FIFTEEN_MINUTES, | |
| mLoggerReceiverPendingIntent); | |
| Toast.makeText(getApplicationContext(), "Repeating Alarm Set", | |
| Toast.LENGTH_LONG).show(); | |
| } | |
| }); | |
| // 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) { | |
| mAlarmManager.setInexactRepeating( | |
| AlarmManager.ELAPSED_REALTIME, | |
| SystemClock.elapsedRealtime() + INITIAL_ALARM_DELAY, | |
| AlarmManager.INTERVAL_FIFTEEN_MINUTES, | |
| mNotificationReceiverPendingIntent); | |
| 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(); | |
| } | |
| }); | |
| // 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) { | |
| mAlarmManager.cancel(mNotificationReceiverPendingIntent); | |
| mAlarmManager.cancel(mLoggerReceiverPendingIntent); | |
| Toast.makeText(getApplicationContext(), | |
| "Repeating Alarms Cancelled", Toast.LENGTH_LONG).show(); | |
| } | |
| }); | |
| } | |
| } | |
| ============================================================== | |
| //AlarmLoggerReceiver.java - Broadcast Receiver | |
| package course.examples.Alarms.AlarmCreate; | |
| 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.i(TAG,"Logging alarm at:" + DateFormat.getDateTimeInstance().format(new Date())); | |
| } | |
| } | |
| ======================================================= | |
| //AlarmNotificationReceiver | |
| package course.examples.Alarms.AlarmCreate; | |
| 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 Uri soundURI = Uri | |
| .parse("android.resource://course.examples.Alarms.AlarmCreate/" | |
| + R.raw.alarm_rooster); | |
| private long[] mVibratePattern = { 0, 200, 200, 300 }; | |
| @Override | |
| public void onReceive(Context context, Intent intent) { | |
| mNotificationIntent = new Intent(context, AlarmCreateActivity.class); | |
| mContentIntent = PendingIntent.getActivity(context, 0, | |
| mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); | |
| 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); | |
| // Pass the Notification to the NotificationManager: | |
| NotificationManager mNotificationManager = (NotificationManager) context | |
| .getSystemService(Context.NOTIFICATION_SERVICE); | |
| mNotificationManager.notify(MY_NOTIFICATION_ID, | |
| notificationBuilder.build()); | |
| Log.i(TAG,"Sending notification at:" + DateFormat.getDateTimeInstance().format(new Date())); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment