Developing Tizen .NET Applications with Alarm Manager

Developing Tizen .NET Applications with Alarm Manager

BY Annie Abraham 2017년 08월 09일 Tizen .NET Application, Tizen .NET

In the previous blog we learnt how to get started with Tizen .NET application, and how to develop a Drumpad application. In this blog we will learn about how to create a simple alarm application using Tizen AlarmManager APIs.

Prerequisites

The following tutorial assumes that you understand the structure of Tizen .NET application, and how to design UI using XAML file. If not, refer to the Drumpad application.

Steps

The following are the steps involved in creating the alarm application:

  1. Creating Project UI
  2. Creating an Alarm
  3. Binding Alarm Creation to Button Click Event
  4. Adding the Privilege
  5. Running the App

Alarm Application Overview

This sample app is a simpler version of the Tizen Clock sample application. This app uses the AlarmManager APIs to create a simple alarm using Xamarin.Forms portable application.

Using this app, the user can set an alarm with a reminder text. When the alarm is triggered the user will get a notification with the reminder text.

                   

     

Creating Project UI

In this sample app, XAML is used to design the UI in the TizenAlarm (portable) code. The required UI controls are:

  • Label to display the text.
  • DatePicker to select the date when the reminder needs to be triggered.
  • TimePicker to select the time when the reminder needs to be triggered.
  • Slider to set the volume of the reminder.
  • Entry to create reminder text to be displayed with the notification.
  • Button to add an alarm.

The following are the steps to add the above listed UI controls over a StackLayout:

  1. Inside the content page, create a StackLayout to display the items one after another.
  2. Add a label with the text “Select Date:” and a DatePicker to select the date for reminder.
  3. Add a label with the text “Select Time:” and a TimePicker to select the exact time for the reminder.
  4. Add a label “Sound”, add image, and a slider to select the volume of the reminder.
  5. Add an entry control to input reminder text that needs to be displayed with the notification.
  6. Add a button “Add Alarm” which when clicked will add the alarm corresponding to the reminder text.

The following is the code in AlarmPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             x:Class="TizenAlarm.AlarmPage" Title="Alarm Application" NavigationPage.HasNavigationBar="True" BackgroundColor="White">

    <ContentPage.Content>

        <StackLayout BackgroundColor="White" Margin="10,10,10,10">

            <Label x:Name = "dateLabel"  Text = "Select Date:"/>

            <Grid>

                <BoxView Color="WhiteSmoke" />

                <DatePicker x:Name = "alarmDatepicker" BackgroundColor="White" />

            </Grid>

            <Label x:Name = "timeLabel"  Text = "Select Time:"/>

            <Grid>

                <BoxView Color="WhiteSmoke" />

                <TimePicker x:Name = "alarmTimePicker" BackgroundColor="White" />

            </Grid>

            <StackLayout Orientation="Horizontal" Margin="10,10,10,10">

                <Label VerticalTextAlignment="Center" Text="Sound" HorizontalOptions="Start" />

                <Image VerticalOptions="Center" Source="clock_sound_volume_icon.png" />

                <Slider VerticalOptions="Center" x:Name="alarmSoundSlider" Value="0.5" HorizontalOptions="FillAndExpand" Margin="10,10,10,10"/>

            </StackLayout>

            <Grid  Margin="30,30,30,30" >

                <BoxView Color="AliceBlue" />

                <Entry   FontAttributes="Bold" Placeholder="Set Reminder Text" x:Name = "alarmText" WidthRequest="50" HorizontalOptions="FillAndExpand"  Margin="30,30,30,30" />

            </Grid>

            <Button Text="Add Alarm" x:Name="alarmButton" VerticalOptions="EndAndExpand"/>

        </StackLayout>

    </ContentPage.Content>

</ContentPage>

AlarmPage.xaml has a corresponding AlarmPage.xaml.cs file were the events can be handled when the user clicks the Add Alarm button.

Set AlarmPage as the Main Page of the application in TizenAlarm.cs by adding the following code in public App() function:

MainPage = new NavigationPage(new AlarmPage());

Build and run the application.

The following is the screenshot of the app:

 

Creating an Alarm

Alarm API is part of Tizen C # Native APIs, and hence cannot be directly called from the portable application. Using Dependency Service framework of Xamarin.Forms you can call the Tizen project from the portable project.

The following are the steps involved to create an alarm:

  1. Create an interface with the required functionality. For this sample app you need to create an alarm with a given date, time, and notification text. Add the following code in IAlarmSetter.cs:

    using System;
    
    namespace TizenAlarm
    {
        public interface IAlarmSetter
        {
            void SetAlarm(DateTime time, string notifiText);
        }
    }
    

     

  2. Add a class (TizenAlarmSetter.cs) in TizenAlarm.TizenMobile that implements IAlarmSetter interface. Add the following code in TizenAlarmSetter.cs:
    using System;
    using System.Threading.Tasks;
    using Tizen.Applications;
    using Tizen.Applications.Notifications;
    using TizenAlarm.TizenMobile;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(TizenAlarmSetter))]
    namespace TizenAlarm.TizenMobile
    {
        class TizenAlarmSetter : IAlarmSetter
        {
            public void SetAlarm(DateTime time, string notifiText)
            {
                Notification noti = new Notification();
                noti.Title = "Alarm";
                noti.Content = notifiText;
                noti.IsDisplay = true;
                noti.AddStyle(new Notification.ActiveStyle());
                AlarmManager.CreateAlarm(time, noti);
             }
        }
    }
    

     

  3. Register the class with Dependency Service module by adding: "[assembly: Dependency(typeof(TizenAlarmSetter))]". The Dependency Service module will call the corresponding implementation at runtime.

     

  4. Create a notification with the content (reminder text) that is set by the user:
    Notification noti = new Notification();
    noti.Title = "Alarm";
    noti.Content = notifiText;
    noti.IsDisplay = true;

     

  5. You can also set the style of the notification using the AddStyle API . Refer to the Notification APIs for more details.
    noti.AddStyle(new Notification.ActiveStyle());

     

  6. Once the notification is ready, create an alarm using AlarmManager API. This creates the alarm which triggers the notification.
    AlarmManager.CreateAlarm(time, noti);

Binding Alarm Creation to Button Click Event

Once you have created the alarm, the final step is to call the TizenAlarmSetter SetAlarm () method from the TizenAlarm (portable) application. In AlarmPage.xaml.cs file, add click event handler for the button as follows:

        public AlarmPage()
        {
            InitilizeComponent();
            alarmDatepicker.MinimumDate = DateTime.Now;
            alarmButton.Clicked += OnButtonClicked;
            alarmTimePicker.Time = DateTime.Now.TimeOfDay;
        }       

And implement the event handler function as follows:

        public void OnButtonClicked(object sender, EventArgs e)
        {
            double value = alarmSoundSlider.Value;

            DateTime selectedTime = new DateTime(alarmDatepicker.Date.Year,
                alarmDatepicker.Date.Month, alarmDatepicker.Date.Day,
                alarmTimePicker.Time.Hours, alarmTimePicker.Time.Minutes
                , alarmTimePicker.Time.Seconds);

            if (selectedTime.CompareTo(DateTime.Now) < 0)
                DisplayAlert("Alert", "Select time greater than current time!", "OK");
            else
            {
                DependencyService.Get<IAlarmSetter>().SetAlarm(selectedTime, alarmText.Text);
                DisplayAlert("Alert", "Reminder Added with Text : \"" + alarmText.Text + "\"" , "OK");
            }
            alarmText.Text = "";
        }

 

Adding the Privilege

This app requires the following privileges:

You can add this privilege to the tizen-manifest.xml file.

 

Running the App

Press CTRL+F5 to launch the application on the device/emulator, and

  • Select a date and time
  • Set a reminder text
  • Click on the button “Add Alarm” to set the alarm

When the alarm is triggered a notification with the reminder text is displayed as follows:

For more details, refer to the attached source code.

File attachments: 
Written by Annie Abraham
Keep Reading All Posts
Read More
Read More
Read More