What are the differences between AlarmClock and AlarmManager?
What are the differences between AlarmClock and AlarmManager?
Classes have different purposes:
AlarmClock provides a set of constants whose function is to facilitate the creation of Intent to launch / control an application on the device that responds to Intent ACTION_SET_ALARM
and ACTION_SET_TIMER
.
The following code calls the default application, or prompts you to choose one, and logs an alarm for 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 you to register an Intent to launch your Activity ) service, or BroadcastReceiver , at a particular day and time.
The following code sets an alarm to launch a BroadcastReceiver every day at 10 am:
//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 AlarmManager you can create an alarm-type application so that it can be launched / controlled by Intents created with AlarmClock .
Although they are two totally distinct classes, here's a brief explanation of each:
{ public end class AlarmClock }
Contains a set of constants and extras that can be used to start an activity to set a new alarm in an "alarm clock" application.
Example:
AlarmClock.EXTRA_HOUR
: Alarm time to be adjusted AlarmClock.EXTRA_MINUTES
: Alarm minute to be set AlarmClock.EXTRA_DAYS
: Alarm days to be adjusted AlarmClock.EXTRA_MESSAGE
: Alarm Message AlarmClock.EXTRA_RINGTONE
: One touch to be triggered in the alarm AlarmClock.EXTRA_VIBRATE
: Will vibrate How it is used:
Intent alarme = new Intent(AlarmClock.ACTION_SET_ALARM);
alarme.putExtra(AlarmClock.EXTRA_HOUR, 10);
startActivity(alarme);
{ public class AlarmManager }
Allows you to schedule your application to run at some future time. When an alarm goes off, an alert is transmitted by the system, then your application responds to that transmission intent and performs an action, such as opening the application, notifying the user via notification in the status bar, or performing another type of action.
In many types of applications, we need some action to be scheduled to run some time later, or to be done periodically outside the life cycle of your application. To do this, the Android system makes the Alarm
feature available through the AlarmManager
class that uses the system resources better than if we had controlled it through or Timer
.
How it 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 AlarmClock documentation and AlarmManager .