Introduction

In today's article, we will make our own Alarm clock. You will be able to hear the alarm sound after 15 seconds and the application will end on click of a button.
Step 1
Open "activity_main.xml" and add the following code to it: (Inside LinearLayout element)
  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="@string/hello_world"
  5. android:textSize="30dp"/>
The layout looks like:
im1.jpg
Step 2
Right-click on layout-> New-> Layout resource file. Name this file as "alarm_layout". This file is to display a button to stop the alarm. Add the following code to this file:
  1. <Button
  2. android:id="@+id/alarm"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="Stop"
  6. android:layout_marginTop="200dp"
  7. android:layout_marginLeft="140dp"
  8. android:background="@drawable/button_lay"/>
The layout looks like:
im2.jpg
Step 3
Make a new layout file and name it as "bye_layout". Add the following code to it: (Inside LinearLayout element)
  1. <TextView
  2. android:text="Thanxx for visiting the Alarm clock..."
  3. android:layout_height="wrap_content"
  4. android:layout_width="fill_parent"
  5. android:textSize="30dp"
  6. android:layout_marginTop="180dp"
  7. android:textColor="#FFFFFF"
  8. android:paddingLeft="20dp"
  9. android:paddingRight="20dp"/>
The layout looks like:
im3.jpg
Step 4
Open "MainActivity.java" and add the following code to it:
  1. package com.alarmclock;
  2. import android.app.AlarmManager;
  3. import android.app.PendingIntent;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.app.Activity;
  8. import android.view.Menu;
  9. import android.view.View;
  10. import android.view.Window;
  11. import android.widget.Button;
  12. import java.util.Calendar;
  13. public class MainActivity extends Activity {
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  18. setContentView(R.layout.activity_main);
  19. Calendar t = Calendar.getInstance();
  20. t.add(Calendar.SECOND, 15);
  21. Intent i = new Intent(this, AlarmSound.class);
  22. PendingIntent pending = PendingIntent.getActivity(this, 1235, i, PendingIntent.FLAG_CANCEL_CURRENT);
  23. AlarmManager alarm = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
  24. alarm.set(AlarmManager.RTC_WAKEUP, t.getTimeInMillis(), pending);
  25. }
  26. @Override
  27. public boolean onCreateOptionsMenu(Menu menu) {
  28. getMenuInflater().inflate(R.menu.main, menu);
  29. return true;
  30. }
  31. }
In the above code activity, "AlarmC" class will be called after 15 seconds. "PendingIntent.FLAG_CANCEL_CURRENT" ensures that after 15 seconds the current activity will be canceled and the new activity ie "AlarmSound" is started. "AlarmManager" allows access to system alarm services. This class allows you to schedule your application to run at some time in the future.
Step 5
Right click on the package-> New-> Java class. Name this class as "AlarmSound" and add the following code to it:
  1. package com.alarmclock;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.media.AudioManager;
  6. import android.media.MediaPlayer;
  7. import android.media.RingtoneManager;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.view.Window;
  14. import android.view.WindowManager;
  15. import android.widget.Button;
  16. import java.io.IOException;
  17. public class AlarmSound extends Activity {
  18. private MediaPlayer player;
  19. final Context context=this;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  24. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  25. setContentView(R.layout.alarm_layout);
  26. Button stop = (Button) findViewById(R.id.alarm);
  27. stop.setOnTouchListener(new View.OnTouchListener() {
  28. public boolean onTouch(View arg0, MotionEvent arg1) {
  29. player.stop();
  30. Intent i=new Intent(context,Bye.class);
  31. startActivity(i);
  32. return false;
  33. }
  34. });
  35. play(this, getAlarmSound());
  36. }
  37. private void play(Context context, Uri alert) {
  38. player = new MediaPlayer();
  39. try {
  40. player.setDataSource(context, alert);
  41. final AudioManager audio = (AudioManager) context
  42. .getSystemService(Context.AUDIO_SERVICE);
  43. if (audio.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
  44. player.setAudioStreamType(AudioManager.STREAM_ALARM);
  45. player.prepare();
  46. player.start();
  47. }
  48. } catch (IOException e) {
  49. Log.e("Error....","Check code...");
  50. }
  51. }
  52. private Uri getAlarmSound() {
  53. Uri alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  54. if (alertSound == null) {
  55. alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  56. if (alertSound == null) {
  57. alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
  58. }
  59. }
  60. return alertSound;
  61. }
  62. }
"getAlarmSound()" returns the alarm sound set in the device. If the alarm tone is not set, the default tone will be returned. "play()" will start the Media Player that plays the sound. Also if "stop" button is clicked, a new activity called "bye" will be called and this will end our application.
Step 6
Create a new Java class and name it as "Bye". Add the following code to it:
  1. package com.alarmclock;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class Bye extends Activity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.bye_layout);
  9. }
  10. }
Step 7
Finally, make the following changes in "AndroidManifest.xml"
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.alarmclock"
  3. android:versionCode="1"
  4. android:versionName="1.0" >
  5. <uses-permission android:name="android.permission.INTERNET" />
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme"
  14. android:debuggable="true">
  15. <activity
  16. android:name="com.alarmclock.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. <activity
  24. android:name="com.alarmclock.AlarmSound"
  25. android:label="@string/app_name" >
  26. </activity>
  27. <activity
  28. android:name=".Bye"
  29. android:label="Byee"/>
  30. </application>
  31. </manifest>
Note that we have added the internet permission.
Step 8
This application will not run on the emulator because the emulator does not consist of the ringtones. Even if you will try the project on the emulator you will not be able to hear the sound. So it's better to run the application on a device.
To run the application on an android phone, install the "apk" of the application on your device. "apk" of your application is present in "Build"-> "Apk". To install the apk, mail the mail to your Gmail account. Go to settings in your phone-> Security->Allow non-market apps. If you then access your account from the native Gmail app on the phone it will recognize that the attachment is an app and offers an "Install" button. Install the app. After the installation completes, run the application.
The output snapshots:
im4.jpg
After 15 sec you will see the screen shown below. This is the time you will hear the alarm sound until you press the stop button.
im5.jpg
Clicking the stop button stops the alarm and gives the following screen
im6.jpg
Thank you... Enjoy coding :)