Simple Alarm Application in Android Studio

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(this1235, 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.  }  

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.    
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.media.AudioManager;  
  7. import android.media.MediaPlayer;  
  8. import android.media.RingtoneManager;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.MotionEvent;  
  13. import android.view.View;  
  14. import android.view.Window;  
  15. import android.view.WindowManager;  
  16. import android.widget.Button;  
  17. import java.io.IOException;  
  18.    
  19. public class AlarmSound extends Activity {  
  20.     private MediaPlayer player;  
  21.     final Context context=this;  
  22.    
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  27.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  28.         setContentView(R.layout.alarm_layout);  
  29.    
  30.         Button stop = (Button) findViewById(R.id.alarm);  
  31.         stop.setOnTouchListener(new View.OnTouchListener() {  
  32.             public boolean onTouch(View arg0, MotionEvent arg1) {  
  33.                 player.stop();  
  34.                 Intent i=new Intent(context,Bye.class);  
  35.                 startActivity(i);  
  36.                 return false;  
  37.             }  
  38.         });  
  39.    
  40.         play(this, getAlarmSound());  
  41.     }  
  42.    
  43.     private void play(Context context, Uri alert) {  
  44.         player = new MediaPlayer();  
  45.         try {  
  46.             player.setDataSource(context, alert);  
  47.             final AudioManager audio = (AudioManager) context  
  48.                     .getSystemService(Context.AUDIO_SERVICE);  
  49.             if (audio.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {  
  50.                 player.setAudioStreamType(AudioManager.STREAM_ALARM);  
  51.                 player.prepare();  
  52.                 player.start();  
  53.             }  
  54.         } catch (IOException e) {  
  55.             Log.e("Error....","Check code...");  
  56.         }  
  57.     }  
  58.    
  59.     private Uri getAlarmSound() {  
  60.         Uri alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);  
  61.         if (alertSound == null) {  
  62.             alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
  63.             if (alertSound == null) {  
  64.                 alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);  
  65.             }  
  66.         }  
  67.         return alertSound;  
  68.     }  

"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.    
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.    
  6. public class Bye extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.bye_layout);  
  11.     }  

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.    
  10.   <application  
  11.       android:allowBackup="true"  
  12.       android:icon="@drawable/ic_launcher"  
  13.       android:label="@string/app_name"  
  14.       android:theme="@style/AppTheme"  
  15.       android:debuggable="true">  
  16.     <activity  
  17.         android:name="com.alarmclock.MainActivity"  
  18.         android:label="@string/app_name" >  
  19.       <intent-filter>  
  20.         <action android:name="android.intent.action.MAIN" />  
  21.    
  22.         <category android:name="android.intent.category.LAUNCHER" />  
  23.       </intent-filter>  
  24.     </activity>  
  25.     <activity  
  26.             android:name="com.alarmclock.AlarmSound"  
  27.             android:label="@string/app_name" >  
  28.     </activity>  
  29.     <activity  
  30.         android:name=".Bye"  
  31.         android:label="Byee"/>  
  32.   </application>  
  33.    
  34. </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 :)


Similar Articles