What is the difference between Alarmclock and Alarmmanager?

Asked

Viewed 404 times

5

What are the differences between Alarmclock and Alarmmanager?

  • Just so you know, which one of the categories fits the bad response part?

2 answers

4


Classes have different purposes:

Alarmclock provides a set of constants whose function is to facilitate the creation of a Intent to launch/control an application, existing on the device, that responds to Intent ACTION_SET_ALARM and ACTION_SET_TIMER.

The following code calls the default application, or asks to choose one, and registers an alarm at 15:30.

Intent createAlarm = new Intent(AlarmClock.ACTION_SET_ALARM);
createAlarm.putExtra(AlarmClock.EXTRA_HOUR, 15);
createAlarm.putExtra(AlarmClock.EXTRA_MINUTES, 30);
startActivity(createAlarm);

Alarmmanager is a class that allows registering a Intent to launch its application(Activity), service or Broadcastreceiver, at a given day and time.

The following code sets an alarm to launch a Broadcastreceiver every day at 10 o'clock:

//Definir a hora de início
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Intent tarefaIntent = new Intent(context, ExecutarTarefaProgramadaReceiver.class);
PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(context,1234, tarefaIntent,0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//Definir o alarme para acontecer todos os dias às 10 horas
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                 AlarmManager.INTERVAL_DAY, tarefaPendingIntent);  

Some examples that use Alarmmanger:

Using the Alarmmanager can create an application like "alarm clock" so it can be launched/controlled by Intents created with the help of Alarmclock.

3

Although they are two totally different classes, see below for a brief explanation of each one:

Alarmclock

{public final class Alarmclock }

Contains a set of constants and extras that can be used to start a Activity to set a new alarm in an "alarm clock app".

Example:

  • AlarmClock.EXTRA_HOUR: Time of alarm to be set
  • AlarmClock.EXTRA_MINUTES: Alarm minute to be set
  • AlarmClock.EXTRA_DAYS: Alarm days to be set
  • AlarmClock.EXTRA_MESSAGE: Alarm setting
  • AlarmClock.EXTRA_RINGTONE: One tap to be triggered on the alarm
  • AlarmClock.EXTRA_VIBRATE: Will vibrate
  • others

How is used:

Intent alarme = new Intent(AlarmClock.ACTION_SET_ALARM);
alarme.putExtra(AlarmClock.EXTRA_HOUR, 10);
startActivity(alarme);

Alarmmanager

{public class Alarmmanager }

Allows you to schedule your application to run at some future time. When an alarm goes off, an intensity is transmitted by the system, so your application responds to this broadcast intention and executes an action, such as opening the application, notifying the user via notification in the status or take other action.

In various types of applications, we need some action to be scheduled to be executed some time later, or to be done periodically outside the lifecycle of your application. For this the Android system makes available the feature of Alarm through the class AlarmManager which uses the resources of the system better than if we had controlled through or Timer.

How is used:

AlarmManager alarme=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Intent i=new Intent(context, OnAlarmReceiver.class); 
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
alarme.setRepeating(
    AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);

For more details, see the documentation on Alarmclock and Alarmmanager.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.