Alarm
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 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 the following:
-
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, follow these steps:
-
To use the Tizen.Applications.Alarm and Tizen.Applications.AlarmManager classes, the application has to request permission by adding the following privileges to the
tizen-manifest.xml
file:XMLCopy<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 namespace in your application:C#Copyusing Tizen.Applications;
-
To use the methods and properties of the Tizen.Applications.Notifications namespace to make an alarm send a notification, include the namespace in your application:
C#Copyusing Tizen.Applications.Notifications;
Set 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:
NoteSince Tizen 6.0, the time period value of an alarm can be one of the values of
Tizen.Applications.AlarmStandardPeriod
. For theCreateAlarm()
method of theTizen.Applications.AlarmManager
class, if you useTizen.Applications.AlarmStandardPeriod
, the time period value of the alarm is guaranteed. IfTizen.Applications.AlarmStandardPeriod
is not used, the time period value will be phase-aligned with another time period value of the alarm.
-
To set an alarm to launch an application, follow these steps:
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 the following processes are done:
-
To identify which application to start when the alarm expires, the Tizen.Applications.AlarmManager class needs an application control instance.
Create a new instance of the Tizen.Applications.AppControl 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:C#Copyint 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 class:
C#CopyAlarm myAlarm = AlarmManager.CreateAlarm(DELAY, PERIOD, appControl);
-
-
When the alarm expires, it triggers the
OnAppControlReceived()
event handler of the Tizen.Applications.CoreApplication class in the AlarmTarget application:C#Copyprotected override void OnAppControlReceived(AppControlReceivedEventArgs e) { base.OnAppControlReceived(e); }
-
-
To set an alarm to send a notification to the user, follow these steps:
-
Create a notification to be sent to the user as an instance of the Tizen.Applications.Notifications.Notification class:
C#Copyint 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:C#CopyAlarm myAlarm = AlarmManager.CreateAlarm(DELAY, PERIOD, myNoti);
-
Set an alarm for a specific date
To schedule an alarm for a specific date, use the CreateAlarm()
method of the Tizen.Applications.AlarmManager 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):
C#
Copy
DateTime dt = AlarmManager.GetCurrentTime();
myAlarm = AlarmManager.CreateAlarm(dt.AddSeconds(20), appControl);
Set 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 class, with values of the Tizen.Applications.AlarmWeekFlag 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:
C#
Copy
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);
List all scheduled alarms and cancel an alarm
You can list all scheduled alarms, and cancel alarms either one by one or all at once by following these steps:
-
To list all scheduled alarms, use the
GetAllScheduledAlarms()
method of the Tizen.Applications.AlarmManager class:C#CopyList<Alarm> alarms; alarms = (List<Alarm>)AlarmManager.GetAllScheduledAlarms();
-
To cancel a single scheduled alarm, use the
Cancel()
method of the Tizen.Applications.Alarm class:C#CopyTizen.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:C#CopyAlarmManager.CancelAll();
Related information
- Dependencies
- Tizen 4.0 and Higher