달력

5

« 2024/5 »

  • 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
728x90
반응형

Android Basics – Alarm Service

February 3rd, 2009 Leave a comment Go to comments

Today we will see Alarm Service provided by Android. Android provide AlarmManager class. The class provides access to the android Alarm Service. AlarmManager provide the methods to set the alarms. Alarm can be one time or repeating. When the alarms goes off (alarm time occurs) a pending intent will be broadcasted that can invoke a BroadCastReceiver (intent receiver) or Service or Activity.

Let’s see how to set alarms. The Alarm Manger instance for the Alarm service can be obtained as follows:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

There are 4 types of alarms the AlarmManager has defined constants for each type as RTC, RTC_WAKEUP, ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP.

While setting an alarm, a pending intent has to be defined. The pending intent is called when alarm goes off.
One time alarms:
One time alarm can be set using AlarmManager set() method. Here is how it can be done:

Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

The pending intent is created for a Broadcast Receiver, the receiver is just notifying the user using Toast. The alarm is set to 10 secs from current time. Here is the code for the intent receiver:

public class OnetimeAlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}

Repeating alarm:
Setting a repeating alarm is similar to setting one time alarm. The repeating alarm can be set using setRepeating() method of AlarmManager. Here is the code:

Intent intent = new Intent(this, RepeatingAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), 10 * 1000, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

There is only one parameter extra in case of repeating alarm. The repeating alarm takes interval after which the alarm should be repeated. In this case the alarm is repeating itself after 10 secs.

The code of the RepeatingAlarmReceiver is:

public class RepeatingAlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();
}
}

The repeating alarms have to be cancelled to stop them. AlarmManager provide a cancel() method that requires the same intent class with which the intent is created. This is how you can cancel the alarm.

alarmManager.cancel(pendingIntent);

Note the pendingIntent object does not need to be same object. The intent fields like action, class, category etc should be same while creating the alarm. The intent is used to identify the alarm to cancel it.

E.g. Alarm Clock:
You can create a wake up alarm with the help of android Alarm service and notification service. The ring tone and the vibration pattern can be put in the intent of extras that can be retrieved by the Broadcast receiver. The code will be as follows:

Intent intent = new Intent(this, RepeatingAlarmReceiver.class);

intent.putExtra("Ringtone", Uri.parse("file:///sdcard/audiofile.mp3"));
intent.putExtra("vibrationPatern", new long[] { 200, 300 });
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), (24 * 60 * 60 * 1000), pendingIntent);

The alarm is set as daily (will repeat after 24 hours). The broadcast receiver can show a notification with the help of the notification service.

NotificationManager manger = (NotificationManager)     context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
notification.setLatestEventInfo(context, "Context Title", "Context text", contentIntent);
notification.flags = Notification.FLAG_INSISTENT;

notification.sound = (Uri) intent.getParcelableExtra("Ringtone");
notification.vibrate = (long[]) intent.getExtras().get("vibrationPatern");

// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID, notification);

You can set appropriate values for contentIntent as your need. You can also pass an array of boolean for each day like Sunday, Monday etc. The intent receiver will match the current day with the alarm days and show the alarm notification accordingly.

728x90
반응형
:
Posted by mapagilove