Android Ultimate Plugin Tutorial part 11 – Alarm Clock

Hi Guys, we just released our new version of Android Ultimate Plugin and one of the new added features is Alarm Clock and now I’m going to show you on how to use this alarm clock feature.

First we need to prepare our Android Manifest file and  make sure to add this permission before Application tag “<application>”

<uses-permission android:name='android.permission.WAKE_LOCK'/>

this permission will make sure that you android device will wake up when it’s sleeping to allow us to fire the alarm service.

Second Make sure you also added this to your Android Manifest File after the Activity tag. “</activity>”

<receiver android:name="com.gigadrillgames.androidplugin.alarm.AlarmManagerBroadcastReceiver"/>
<service android:name="com.gigadrillgames.androidplugin.alarm.AlarmService" android:enabled="true" android:exported="true"/>

This Receiver tag “AlarmManagerBroadcastReceiver” is the one that will received the event when our alarm time has come, this is also responsible for showing notification and for dispatching the service  which in our case “AlarmService”  that will play the alarm sound. without this two line of tags our alarm clock will not work.

After preparing our Android Manifest File and making sure that the required permission and tags are present and place in the right position. we can start coding now to actually set the alarm clock. let’s create a new C# Script inside Unity3d and add the following line of codes.

First we need to have the instance of Alarm Plugin

private AlarmPlugin alarmPlugin = AlarmPlugin.GetInstance();

Next we need to set if we want to see the Debug Toast Messages pass 1 to show or pass 0 to not show.

alarmPlugin.SetDebug(0);

And Then we initialized it

alarmPlugin.Init();

Here’s the Start method will look like

private AlarmPlugin alarmPlugin;

void Start () {
 alarmPlugin = AlarmPlugin.GetInstance();
 alarmPlugin.SetDebug(0);
 alarmPlugin.Init();
}

After we initialized it we can now set our Alarm, by the way there’s  3 types of alarm one time alarm, repeating alarm and repeating alarm with interval.

1. One Time Alarm  – will be fired only once and it will never be fire again.

2. Repeating Alarm – will be fired once a day.

3. Repeating Alarm – will be fired once then it will be fire again based on your set delay interval ( 1000 = 1 sec ).

To Set One Time Alarm just pass hour, minute, “Alarm Notification Title”, “Alarm Notification Message” and “Alarm Ticker Message”

public void SetOneTimeAlarm(){
  //Hour range  (1 - 24)
  //14 = 2PM
  int hour = 14;

  //Minute Range (1-59)
  int minute = 30;

  alarmPlugin.SetOneTimeAlarm(hour,minute,"Alarm Title - one time      alarm","Alarm Message","Alarm Ticker Message");
}

To set Repeating Alarm just pass hour, minute, “Alarm Notification Title”, “Alarm Notification Message” and “Alarm Ticker Message”

public void SetRepeatingAlarm(){
  //Hour range  (1 - 24)
  //8 = 8AM
  int hour = 8;

  //Minute Range (1-59)
  int minute = 30;

  alarmPlugin.SetRepeatingAlarm(hour,minute,"Alarm Title - repeating   alarm","Alarm Message","Alarm Ticker Message");
}

To set Repeating Alarm with Interval just pass hour, minute, delay interval, “Alarm Notification Title”, “Alarm Notification Message” and “Alarm Ticker Message”

public void SetRepeatingAlarmWithInterval(){
  //Hour range  (1 - 24)
  //16 = 4PM
  int hour = 16;

  int minute = 30;

  // 1000 * 60 = 1 minute
  int delayInterval = (1000 * 60)

alarmPlugin.SetRepeatingAlarmWithInterval(hour,minute,delayInterval,"Alarm Title - repeating alarm with interval","Alarm Message","Alarm Ticker Message");
}

To Cancel Alarm – this will work when the alarm is not yet fired, if you use this when alarm is already fired it will not work.

public void CancelAlarm(){
  alarmPlugin.CancelAlarm();
}

To Stop Alarm – this will work when the alarm is already fired.

public void StopAlarm(){
  alarmPlugin.StopAlarm();
}

And That’s it you can now try it on your android devices

Good luck and Happy Coding!

For more Information

http://www.gigadrillgames.com/android-ultimate-plugin/

For more Tutorials

http://www.gigadrillgames.com/2015/07/26/list-of-tutorials-for-android-ultimate-plugin/

 

for questions or comment or any feedback just email us at gigadrillgames@gmail.com

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.