7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
1/17
T
Android Alarm Clock Tutorial
his Android tutorial will walk you through to create an alarm clock Android
application. This alarm app is planned to be minimalistic and usable. It
can set alarm for one occurrence for the coming day. You will get alarm ring
sound, a notification message and a message in the app UI. This application
and device can be idle or sleeping when the alarm triggers.
http://javapapers.com/7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
2/17
We will be using the AlarmManagerAPI to set and ring the alarm
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
3/17
notification. We will have a TimePicker component and a toggle switch in the UI
to set the alarm time.
AndroidAlarmClock
Android Alarm Clock Application
Application is planned to be simple as possible and you can use this as a base
framework and enhance it by adding fancy features.
1. Android Manifest
AndroidManifest.xml
Freedocast-BroadcastLive(Beta)
FREE
75)
https://www.googleadservices.com/pagead/aclk?sa=L&ai=C_hciV2A0V4vLMdWOvgTjw4yYBrzmwdREnt2v4s0C08To6EcQASD4iPkFYOXS5oO8DqABgviuwQPIAQGoAwHIA5sEqgSYAU_Qjr1q9KjU_roCziNflEm0XfEsTqP4Qiuh1WnDH4ZeO1y6QJdr1QVzn3IaA_MFBqbr5SZCsuQ9Jb_VnozOuxxaMuySSv5UjxnCotN83pfvKzRb7w3qT8PuUO0mZ9bkul42lK-wEalHOyX5LWuufiE3222qqchRZ-CeWiDYFc7BdAugBsrR7jwXYMJeykogcbDRJ6Ll2oAOiAYBoAYasAYB2AYCgAfmh9E-qAemvhvYBwHYEwg&num=1&cid=CAASEuRo9pxU8wgeKGj88j95QFw-nw&sig=AOD64_0-voZ2wSgKVkKbsNj7JHQPL6l-jw&client=ca-pub-2525233393528874&adurl=market://details%3Fid%3Dcom.freedocast.live%26referrer%3Dnetwork%253Dd%2526campaignid%253D361599006%2526loc_physical_ms%253D1007785http://javapapers.com/wp-content/uploads/2014/12/AndroidAlarmClock.zip7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
4/17
We need to give uses-permission for WAKE_LOCK, other than that the
AndroidManifest.xml is pretty standard one. Just need to include the service
and receiver.
2. Android Activity
activity_my.xml
< activity>
< application>< manifest>
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
5/17
The Android Activity is designed to be simple. We have a TimePicker
component followed by a ToggleButton. Thats it. Choose the time to set
the alarm and toggle the switch to on. The alarm will work.
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
6/17
AlarmActivity.java
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
7/17
AlarmActivity uses the AlarmManager to set the alarm and send notification on
alarm trigger.
packagecom.javapapers.androidalarmclock;
importandroid.app.Activity;importandroid.app.AlarmManager;importandroid.app.PendingIntent;importandroid.content.Intent;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.TextView;importandroid.widget.TimePicker;importandroid.widget.ToggleButton;
importjava.util.Calendar;
publicclassAlarmActivityextendsActivity{
AlarmManageralarmManager; privatePendingIntentpendingIntent;
privateTimePickeralarmTimePicker;privatestaticAlarmActivityinst;privateTextViewalarmTextView;
publicstaticAlarmActivityinstance(){returninst;
}
@OverridepublicvoidonStart(){
super.onStart();inst =this;
}
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
8/17
3. Alarm Receiver
AlarmReceiver.java
AlarmReceiver is a WakefulBroadcasReceiver, this is the one that
receives the alarm trigger on set time. From here we initiate different actions to
notify the user as per our choice. I have given three type of notifications, first
show a message to user in the activity UI, second play the alarm ringtone and
third send an Android notification message. So this is the place to add
enhancement for different types of user notifications.
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
9/17
4. Alarm Notification Message
packagecom.javapapers.androidalarmclock;
importandroid.app.Activity;importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.media.Ringtone;importandroid.media.RingtoneManager;
importandroid.net.Uri;importandroid.support.v4.content.WakefulBroadcastReceiver;
publicclassAlarmReceiverextendsWakefulBroadcastReceiver
@OverridepublicvoidonReceive(finalContextcontext,Intentint
//this will update the UI with messageAlarmActivityinst =AlarmActivity.instance();
inst.setAlarmText("Alarm! Wake up! Wake up!");
//this will sound the alarm tone//this will sound the alarm once, if you wish to//raise alarm in loop continuously then use MediaPlUrialarmUri =RingtoneManager.getDefaultUri(RingtoneManager
if(alarmUri ==null){alarmUri =RingtoneManager.getDefaultUri(RingtoneManager
}Ringtoneringtone =RingtoneManager.getRingtone(con
ringtone.play();
//this will send a notification messageComponentNamecomp =newComponentName(context.getP
AlarmService.class.getName());startWakefulService(context,(intent.setComponent(c
Activity
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
10/17
AlarmService.java
The receiver will start the following IntentServiceto send a standard
notification to the user.
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
11/17
packagecom.javapapers.androidalarmclock;
importandroid.app.IntentService;importandroid.app.NotificationManager;importandroid.app.PendingIntent;importandroid.content.Context;importandroid.content.Intent;importandroid.support.v4.app.NotificationCompat;
importandroid.util.Log;
publicclassAlarmServiceextendsIntentService{privateNotificationManageralarmNotificationManager;
publicAlarmService(){super("AlarmService");
}
@Override
publicvoidonHandleIntent(Intentintent){sendNotification("Wake Up! Wake Up!");
}
privatevoidsendNotification(Stringmsg){Log.d("AlarmService","Preparing to send notificatialarmNotificationManager =(NotificationManager)this
.getSystemService(Context.NOTIFICATION_SERV
PendingIntentcontentIntent =PendingIntent.getActi
newIntent(this,AlarmActivity.class),0);
NotificationCompat.BuilderalamNotificationBuilderthis).setContentTitle("Alarm").setSmallIcon
.setStyle(newNotificationCompat.BigTextStyle
7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
12/17
PREVIOUS:
Android Geocoding to Get Latitude Longitude for an Address
NEXT:
Dropbox Java API Tutorial
AndroidAlarmClock
This Android tutorial was added on 04/12/2014.
Freedocast-BroadcastLive(Beta)
FREE
75)
https://www.googleadservices.com/pagead/aclk?sa=L&ai=CA8NoWGA0V6fSDY6NvgSsorGIBrzmwdREnt2v4s0C08To6EcQASD4iPkFYOXS5oO8DqABgviuwQPIAQGoAwHIA5sEqgSbAU_Q-YdtP7Rs3vvEb_DGt_GtNJkoxlTgDSZei5JCxeZzhEK0_fBAQ-YOABBUeg3YKba2IR4e3KAc55b-fzWV6HHO_PhC_eYbKBx8nzV10Rzfdf_7oJqV30J2wNPNyN2zkc4KIM8ST5F7vdA3SNKBoAYy5yNlgruMIilR1EWSyzK9pwgfkTkLFPHPmE48aHTM5gPc9XZ02HpBey3_iAYBoAYasAYB2AYCgAfmh9E-qAemvhvYBwHYEwg&num=1&cid=CAASEuRo-ScG5uqE-08-VqTI5B47aA&sig=AOD64_1leaE4i2uXobGrQOKNj5BIm9Ks9g&client=ca-pub-2525233393528874&adurl=market://details%3Fid%3Dcom.freedocast.live%26referrer%3Dnetwork%253Dd%2526campaignid%253D361599006%2526loc_physical_ms%253D1007785http://javapapers.com/wp-content/uploads/2014/12/AndroidAlarmClock.ziphttp://javapapers.com/java/dropbox-java-api-tutorial/http://javapapers.com/android/android-geocoding-to-get-latitude-longitude-for-an-address/7/25/2019 Android Alarm Clock Tutorial - Java Tutorial Blog (1).pdf
13/17
Shaitansays: 30/12/2014 at 6:38 am
Romansays: 21/01/2015 at 6:14 am
Comments on "Android Alarm Clock Tutorial"
Tutorial:
Sorry 4 double-posting :(
I forgot to add the service in the manifest
now it works, but when the receiver recieve the event it throws a exception:
Unable to start receiver de.it2do.effektivlernen.AlarmReceiver:
java.lang.SecurityException: Neither user 10043 nor current process has
android.permission.WAKE_LOCK.
My Manifest.xml has the permission:
Can you help me? >.