Android Alarm Clock Tutorial - Java Tutorial Blog PDF
Android Alarm Clock Tutorial - Java Tutorial Blog PDF
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.
AndroidAlarmClock
1. Android Manifest
AndroidManifest.xml
2. Android Activity
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/alarmTimePicker"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm On/Off"
android:id="@+id/alarmToggle"
android:layout_centerHorizontal="true"
android:layout_below="@+id/alarmTimePicker"
android:onClick="onToggleClicked" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/alarmText"
android:layout_alignParentBottom="true"
AlarmActivity.java
AlarmActivity uses the AlarmManager to set the alarm and send notification on
alarm trigger.
package com.javapapers.androidalarmclock;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;
import java.util.Calendar;
public class AlarmActivity extends Activity {
AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private static AlarmActivity inst;
private TextView alarmTextView;
public static AlarmActivity instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
@Override
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.
package com.javapapers.androidalarmclock;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;
public class AlarmReceiver extends WakefulBroadcastReceiver
@Override
public void onReceive(final Context context, Intent intent
//this will update the UI with message
AlarmActivity inst = AlarmActivity.instance();
inst.setAlarmText("Alarm! Wake up! Wake up!");
AlarmService.java
The receiver will start the following IntentServiceto send a standard
notification to the user.
package com.javapapers.androidalarmclock;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
sendNotification("Wake Up! Wake Up!");
}
private void sendNotification(String msg) {
Log.d("AlarmService", "Preparing to send notification...: "
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE
PendingIntent contentIntent = PendingIntent.getActivity
new Intent(this, AlarmActivity.class), 0);
NotificationCompat.Builder alamNotificationBuilder
this).setContentTitle("Alarm").setSmallIcon
.setStyle(new NotificationCompat.BigTextStyle
.setContentText(msg);
AndroidAlarmClock
P R E V IO U S :
NEXT:
30/12/2014 at 6:38 am
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:
Roman says:
21/01/2015 at 6:14 am
me says:
21/02/2015 at 11:15 am
17/03/2015 at 3:45 pm
Once the alarm starts it never gets stopped. Switching the toggle button doesnt
cancel the sound, rather it turns into a loop and the ring repeats faster.
Why so?
Rihana says:
20/03/2015 at 3:39 am
This app contains lots of bugs. I dont recommend people to follow this tutorial.
ajay says:
06/04/2015 at 10:30 am
Can you show the use of media player to set the alarm tone in continuous loop.
Josh G says:
07/07/2015 at 5:50 am
I am having the same problem as Naif. This code does not include anything to
turn off the alarm.
21/07/2015 at 2:10 pm
My code is Completely done..without error..But when I set the time the alarm
does not work..The music also not sound
Vaishnav says:
28/07/2015 at 3:45 pm
Jerry says:
31/07/2015 at 5:31 am
OMG
Cant run at all
OmenDawg says:
20/10/2015 at 12:35 am
Great job!I was able to get the application up and going within an hour, and this
is my very first attempt to build an Android app. If anyone encounters any
error(s), its more than likely due to syntax/setup related issues. Everything is
here, couldnt be any simplier. Thanks for posting, this was really helpful.
zahra says:
tanx a lot.
very good.
27/10/2015 at 3:32 pm
alex says:
12/11/2015 at 2:49 am
sfwrf says:
21/11/2015 at 12:35 pm
Comments are closed for this "Android Alarm Clock Tutorial" tutorial.
Go to Top
Site Map
JAVA
ANDROID
DESIGN PATTERNS
SPRING
WEB SERVICES
SERVLET