Alarms
PUBLISHED
You can use alarms to launch applications or send user notifications at specific times. The mechanism involved in launching the application is the Tizen.Applications.AppControl [1] class.
The Tizen.Applications.AppControl
class allows launching an application explicitly, giving its package name, or providing certain criteria that the application must meet. For example, the criteria can include the type of data on which the application must be able to operate. The structure containing the criteria is called an application control.
The main features of the Tizen.Applications.Alarm
and Tizen.Applications.AlarmManager
classes include:
- Setting alarms with a specific delay
You can set an alarm to trigger after a specific amount of time.
- Setting alarms for specific dates
You can set an alarm for a specific date.
- Setting recurring alarms
You can set a recurring alarm to trigger on a specific day or days of the week.
- Managing existing alarms
Prerequisites
To enable your application to use the alarm functionality:
- To use the Tizen.Applications.Alarm [2] and Tizen.Applications.AlarmManager [3] classes, the application has to request permission by adding the following privileges to the
tizen-manifest.xml
file:<privileges> <privilege>http://tizen.org/privilege/alarm.get</privilege> <privilege>http://tizen.org/privilege/alarm.set</privilege> <!--If an alarm is used to send a notification--> <privilege>http://tizen.org/privilege/notification</privilege> </privileges>
- To use the methods and properties of the
Tizen.Applications.Alarm
andTizen.Applications.AlarmManager
classes, include the Tizen.Applications [4] namespace in your application:
using Tizen.Applications;
- To use the methods and properties of the Tizen.Applications.Notifications [5] namespace to make an alarm send a notification, include the namespace in your application:
using Tizen.Applications.Notifications;
Setting an Alarm with a Specific Delay
You can set an alarm which, when it expires, either launches an application or sends a notification to the user:
- To set an alarm to launch an application:
You need 2 applications: the "AlarmRegister" application that sets the alarm, and the "AlarmTarget" application that is launched when the alarm expires.
- In the AlarmRegister application:
- To identify which application to start when the alarm expires, the Tizen.Applications.AlarmManager [3] class needs an application control instance.
Create a new instance of the Tizen.Applications.AppControl [1] class, and set the
Operation
andApplicationID
properties for it. TheOperation
property identifies the operation to be performed, and theApplicationID
property identifies theappid
of the target application to be launched. You can get theappid
of the target application from itstizen-manifest.xml
file.int DELAY = 2; int PERIOD = 1; AppControl appControl = new AppControl(); appControl.Operation = AppControlOperations.Default; appControl.ApplicationId = "org.tizen.alarmslave";
- To schedule an alarm after a delay, use the
CreateAlarm()
method of theTizen.Applications.AlarmManager
class, with the initial delay, interval for subsequent alarms, and instance of theTizen.Applications.AppControl
class as parameters.The method creates the alarm as a new instance of the Tizen.Applications.Alarm [2] class.
Alarm myAlarm = AlarmManager.CreateAlarm(DELAY, PERIOD, appControl);
- To identify which application to start when the alarm expires, the Tizen.Applications.AlarmManager [3] class needs an application control instance.
- When the alarm expires, it triggers the
OnAppControlReceived()
event handler of the Tizen.Applications.CoreApplication [6] class in the AlarmTarget application:
protected override void OnAppControlReceived(AppControlReceivedEventArgs e) { base.OnAppControlReceived(e); }
- In the AlarmRegister application:
- To set an alarm to send a notification to the user:
- Create a notification to be sent to the user as an instance of the Tizen.Applications.Notifications.Notification [7] class:
int DELAY = 2; int PERIOD = 1; Notification myNoti; myNoti = new Notification { Title = "Notification", Content = "Hello Tizen" };
- To schedule an alarm after a delay, use the
CreateAlarm()
method of theTizen.Applications.AlarmManager
class, with the initial delay, interval for subsequent alarms, and instance of theTizen.Applications.Notifications.Notification
class as parameters.The method creates the alarm as a new instance of the
Tizen.Applications.Alarm
class.Alarm myAlarm = AlarmManager.CreateAlarm(DELAY, PERIOD, myNoti);
- Create a notification to be sent to the user as an instance of the Tizen.Applications.Notifications.Notification [7] class:
Setting an Alarm for a Specific Date
To schedule an alarm for a specific date, use the CreateAlarm()
method of the Tizen.Applications.AlarmManager [3] class with an instance of the DateTime
structure as its first parameter.
The following example schedules an application control to trigger 20 seconds after the current time (using the AddSecond()
method of the DateTime
structure):
DateTime dt = AlarmManager.GetCurrentTime(); myAlarm = AlarmManager.CreateAlarm(dt.AddSeconds(20), appControl);
Setting a Recurring Alarm
You can set a recurring alarm that goes off at a specific moment, and thereafter at regular intervals.
To schedule a recurring alarm to go off on specific days of the week, use the CreateAlarm()
method of the Tizen.Applications.AlarmManager [3] class, with values of the Tizen.Applications.AlarmWeekFlag [8] enumeration as the second parameter. You can join multiple values together to set the alarm to trigger on multiple days of the week.
The following example schedules an application control to be invoked at a set time every Tuesday and Friday:
Tizen.Applications.AppControl appControl = new Tizen.Applications.AppControl(); appControl.Operation = AppControlOperations.Default; appControl.ApplicationId = "org.tizen.alarmslave" Alarm myAlarm = AlarmManager.CreateAlarm(DateTime.New.AddSecond(10), AlarmWeekFlag.Tuesday | AlarmWeekFlag.Friday, appControl);
Listing All Scheduled Alarms and Canceling an Alarm
You can list all scheduled alarms, and cancel alarms either one by one or all at once:
- To list all scheduled alarms, use the
GetAllScheduledAlarms()
method of the Tizen.Applications.AlarmManager [3] class:List<Alarm> alarms; alarms = (List<Alarm>)AlarmManager.GetAllScheduledAlarms();
- To cancel a single scheduled alarm, use the
Cancel()
method of the Tizen.Applications.Alarm [2] class:
Tizen.Applications.AppControl appControl = new Tizen.Applications.AppControl(); Alarm myAlarm; appControl.Operation = AppControlOperations.Default; appControl.ApplicationId = "org.tizen.alarmslave"; myAlarm = AlarmManager.CreateAlarm(100, appControl); try { myAlarm.Cancel(); } catch (Exception e) { Log.Error("ALARM", "Exception occurs"); }
- To cancel all alarms registered by the application, use the
CancelAll()
method of theTizen.Applications.AlarmManager
class:
AlarmManager.CancelAll();