Create Alarm Android Application

Introduction

Android is one of the most popular operating systems for mobiles. Alarm app takes place in day by day Android usage. Each Android mobile contains alarm application. Simply alarm is our wake-up assistant. So, I will show you How to create alarm android application using Android Studio. Android is the kernel based operating system.it allows the user can modify the GUI components and source code.

Requirements

  • Android Studio
  • Little bit XML and JAVA knowledge.
  • Android Emulator (or) Android mobile

Download link (Android Studio) : https://developer.android.com/studio/index.html

Steps should be followed

Carefully follow my steps to create Alarm Android Application using Android Studio. I have included the source code below.

Step 1

Open the Android Studio and start new project.

Android

Step 2

Put the application name and company domain. If you wish to use C++ for coding the project , mark the "Include C++ support" checkbox and then click Next.

Android

Step 3

Select the Android minimum SDK. After you chose the minimum SDK, it will show approximate percentage of people who use that SDK. Click Next.

Android

Step 4

Choose the basic activity, then click Next.

Android

Step 5

Put the activity name and layout name. Android Studio basically takes the java class name that you provide as the activity name and click Finish.

Android

Step 6

Go to activity_main.xml then click the text bottom. This xml file contains the designing code for android app. Into the activity_main.xml copy and paste the below code.

Activity_main.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:fitsSystemWindows="true"  
  8.     tools:context="ganeshannt.alarm.MainActivity">  
  9.   
  10.     <android.support.design.widget.AppBarLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:theme="@style/AppTheme.AppBarOverlay">  
  14.   
  15.         <android.support.v7.widget.Toolbar  
  16.             android:id="@+id/toolbar"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="?attr/actionBarSize"  
  19.             android:background="?attr/colorPrimary"  
  20.             app:popupTheme="@style/AppTheme.PopupOverlay" />  
  21.   
  22.     </android.support.design.widget.AppBarLayout>  
  23.   
  24.     <include layout="@layout/content_main" />  
  25.   
  26. </android.support.design.widget.CoordinatorLayout>  

Android

Step 7

Create new content_main.xml file (File ⇒ New ⇒Activity⇒Empty_activity).

Go to content_main.xml then click the text bottom. This xml file contains the designing code for android app. Into the content_main.xml copy and paste the below code

content_main.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:fitsSystemWindows="true"  
  8.     tools:context="ganeshannt.alarm.MainActivity">  
  9.   
  10.     <android.support.design.widget.AppBarLayout  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:theme="@style/AppTheme.AppBarOverlay">  
  14.   
  15.         <android.support.v7.widget.Toolbar  
  16.             android:id="@+id/toolbar"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="?attr/actionBarSize"  
  19.             android:background="?attr/colorPrimary"  
  20.             app:popupTheme="@style/AppTheme.PopupOverlay" />  
  21.   
  22.     </android.support.design.widget.AppBarLayout>  
  23.   
  24.     <include layout="@layout/content_main" />  
  25.   
  26. </android.support.design.widget.CoordinatorLayout>  

Android

Step 8

Into the MainActivity.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise app will not run.

MainActivity.java code

  1. package ganeshannt.alarm;  
  2.   
  3. import android.annotation.TargetApi;  
  4. import android.app.AlarmManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.os.Build;  
  9. import android.os.Bundle;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.support.v7.widget.Toolbar;  
  12. import android.util.Log;  
  13. import android.view.Menu;  
  14. import android.view.MenuItem;  
  15. import android.view.View;  
  16. import android.widget.AdapterView;  
  17. import android.widget.ArrayAdapter;  
  18. import android.widget.Button;  
  19. import android.widget.Spinner;  
  20. import android.widget.TextView;  
  21. import android.widget.TimePicker;  
  22.   
  23. import java.util.Calendar;  
  24.   
  25.   
  26. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {  
  27.   
  28.     //to make our alarm manager  
  29.     AlarmManager alarm_manager;  
  30.     TimePicker alarm_timepicker;  
  31.     TextView update_text;  
  32.     Context context;  
  33.     PendingIntent pending_intent;  
  34.     int choose_whale_sound;  
  35.   
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.activity_main);  
  41.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  42.         setSupportActionBar(toolbar);  
  43.         this.context = this;  
  44.   
  45.         // initialize our alarm manager  
  46.         alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);  
  47.   
  48.         //initialize our timepicker  
  49.         alarm_timepicker = (TimePicker) findViewById(R.id.timePicker);  
  50.   
  51.         //initialize our text update box  
  52.         update_text = (TextView) findViewById(R.id.update_text);  
  53.   
  54.         // create an instance of a calendar  
  55.         final Calendar calendar = Calendar.getInstance();  
  56.   
  57.         // create an intent to the Alarm Receiver class  
  58.         final Intent my_intent = new Intent(this.context, Alarm_Receiver.class);  
  59.   
  60.   
  61.         // create the spinner in the main UI  
  62.         Spinner spinner = (Spinner) findViewById(R.id.richard_spinner);  
  63.         // Create an ArrayAdapter using the string array and a default spinner layout  
  64.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,  
  65.                 R.array.whale_array, android.R.layout.simple_spinner_item);  
  66.         // Specify the layout to use when the list of choices appears  
  67.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  68.         // Apply the adapter to the spinner  
  69.         spinner.setAdapter(adapter);  
  70.         // Set an onclick listener to the onItemSelected method  
  71.         spinner.setOnItemSelectedListener(this);  
  72.   
  73.   
  74.         // initialize start button  
  75.         Button alarm_on = (Button) findViewById(R.id.alarm_on);  
  76.   
  77.         // create an onClick listener to start the alarm  
  78.         alarm_on.setOnClickListener(new View.OnClickListener() {  
  79.             @TargetApi(Build.VERSION_CODES.M)  
  80.             @Override  
  81.             public void onClick(View v) {  
  82.   
  83.                 // setting calendar instance with the hour and minute that we picked  
  84.                 // on the time picker  
  85.                 calendar.set(Calendar.HOUR_OF_DAY, alarm_timepicker.getHour());  
  86.                 calendar.set(Calendar.MINUTE, alarm_timepicker.getMinute());  
  87.   
  88.                 // get the int values of the hour and minute  
  89.                 int hour = alarm_timepicker.getHour();  
  90.                 int minute = alarm_timepicker.getMinute();  
  91.   
  92.                 // convert the int values to strings  
  93.                 String hour_string = String.valueOf(hour);  
  94.                 String minute_string = String.valueOf(minute);  
  95.   
  96.                 // convert 24-hour time to 12-hour time  
  97.                 if (hour > 12) {  
  98.                     hour_string = String.valueOf(hour - 12);  
  99.                 }  
  100.   
  101.                 if (minute < 10) {  
  102.                     //10:7 --> 10:07  
  103.                     minute_string = "0" + String.valueOf(minute);  
  104.                 }  
  105.   
  106.                 // method that changes the update text Textbox  
  107.                 set_alarm_text("Alarm set to: " + hour_string + ":" + minute_string);  
  108.   
  109.                 // put in extra string into my_intent  
  110.                 // tells the clock that you pressed the "alarm on" button  
  111.                 my_intent.putExtra("extra""alarm on");  
  112.   
  113.                 // put in an extra int into my_intent  
  114.                 // tells the clock that you want a certain value from the drop-down menu/spinner  
  115.                 my_intent.putExtra("whale_choice", choose_whale_sound);  
  116.                 Log.e("The whale id is" , String.valueOf(choose_whale_sound));  
  117.   
  118.                 // create a pending intent that delays the intent  
  119.                 // until the specified calendar time  
  120.                 pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,  
  121.                         my_intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  122.   
  123.                 // set the alarm manager  
  124.                 alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),  
  125.                         pending_intent);  
  126.   
  127.             }  
  128.   
  129.   
  130.   
  131.         });  
  132.   
  133.   
  134.   
  135.         // initialize the stop button  
  136.         Button alarm_off = (Button) findViewById(R.id.alarm_off);  
  137.         // create an onClick listener to stop the alarm or undo an alarm set  
  138.   
  139.         alarm_off.setOnClickListener(new View.OnClickListener() {  
  140.             @Override  
  141.             public void onClick(View v) {  
  142.   
  143.                 // method that changes the update text Textbox  
  144.                 set_alarm_text("Alarm off!");  
  145.   
  146.                 // cancel the alarm  
  147.                 alarm_manager.cancel(pending_intent);  
  148.   
  149.                 // put extra string into my_intent  
  150.                 // tells the clock that you pressed the "alarm off" button  
  151.                 my_intent.putExtra("extra""alarm off");  
  152.                 // also put an extra int into the alarm off section  
  153.                 // to prevent crashes in a Null Pointer Exception  
  154.                 my_intent.putExtra("whale_choice", choose_whale_sound);  
  155.   
  156.   
  157.                 // stop the ringtone  
  158.                 sendBroadcast(my_intent);  
  159.   
  160.   
  161.             }  
  162.         });  
  163.   
  164.   
  165.   
  166.     }  
  167.   
  168.     private void set_alarm_text(String output) {  
  169.         update_text.setText(output);  
  170.     }  
  171.   
  172.     @Override  
  173.     public boolean onCreateOptionsMenu(Menu menu) {  
  174.         // Inflate the menu; this adds items to the action bar if it is present.  
  175.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  176.         return true;  
  177.     }  
  178.   
  179.     @Override  
  180.     public boolean onOptionsItemSelected(MenuItem item) {  
  181.         // Handle action bar item clicks here. The action bar will  
  182.         // automatically handle clicks on the Home/Up button, so long  
  183.         // as you specify a parent activity in AndroidManifest.xml.  
  184.         int id = item.getItemId();  
  185.   
  186.         //noinspection SimplifiableIfStatement  
  187.         if (id == R.id.action_settings) {  
  188.             return true;  
  189.         }  
  190.   
  191.         return super.onOptionsItemSelected(item);  
  192.     }  
  193.   
  194.     @Override  
  195.     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  196.         // An item was selected. You can retrieve the selected item using  
  197.         // parent.getItemAtPosition(pos)  
  198.   
  199.         // outputting whatever id the user has selected  
  200.         //Toast.makeText(parent.getContext(), "the spinner item is "  
  201.         //        + id, Toast.LENGTH_SHORT).show();  
  202.         choose_whale_sound = (int) id;  
  203.   
  204.   
  205.     }  
  206.   
  207.     @Override  
  208.     public void onNothingSelected(AdapterView<?> parent) {  
  209.         // Another interface callback  
  210.   
  211.     }  
  212. }  
Step 9

Create new Alarm_Receiver.java file (File ⇒ New ⇒Java class).

Into the Alarm_Receiver.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise app will not run.

Alarm_Receiver.java code

  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.util.Log;  
  5.   
  6.   
  7. public class Alarm_Receiver  extends BroadcastReceiver{  
  8.   
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.   
  13.         Log.e("We are in the receiver.""Yay!");  
  14.   
  15.         // fetch extra strings from the intent  
  16.         // tells the app whether the user pressed the alarm on button or the alarm off button  
  17.         String get_your_string = intent.getExtras().getString("extra");  
  18.   
  19.         Log.e("What is the key? ", get_your_string);  
  20.   
  21.         // fetch the extra longs from the intent  
  22.         // tells the app which value the user picked from the drop down menu/spinner  
  23.         Integer get_your_whale_choice = intent.getExtras().getInt("whale_choice");  
  24.   
  25.         Log.e("The whale choice is ", get_your_whale_choice.toString());  
  26.   
  27.         // create an intent to the ringtone service  
  28.         Intent service_intent = new Intent(context, RingtonePlayingService.class);  
  29.   
  30.         // pass the extra string from Receiver to the Ringtone Playing Service  
  31.         service_intent.putExtra("extra", get_your_string);  
  32.         // pass the extra integer from the Receiver to the Ringtone Playing Service  
  33.         service_intent.putExtra("whale_choice", get_your_whale_choice);  
  34.   
  35.         // start the ringtone service  
  36.         context.startService(service_intent);  
  37.   
  38.     }  
  39.   
  40. }  
Step 10 

Create new RingtonePlayingService.java file (File ⇒ New ⇒Java class).

Into the RingtonePlayingService.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise app will not run.

RingtonePlayingService.java code

  1. package ganeshannt.alarm;  
  2. import android.annotation.TargetApi;  
  3. import android.app.Notification;  
  4. import android.app.NotificationManager;  
  5. import android.app.PendingIntent;  
  6. import android.app.Service;  
  7. import android.content.Intent;  
  8. import android.media.MediaPlayer;  
  9. import android.os.Build;  
  10. import android.os.IBinder;  
  11. import android.util.Log;  
  12.   
  13. import java.util.Random;  
  14.   
  15.   
  16. public class RingtonePlayingService extends Service {  
  17.   
  18.     MediaPlayer media_song;  
  19.     int startId;  
  20.     boolean isRunning;  
  21.   
  22.   
  23.     @Override  
  24.     public IBinder onBind(Intent intent) {  
  25.         return null;  
  26.     }  
  27.   
  28.     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
  29.     @Override  
  30.     public int onStartCommand(Intent intent, int flags, int startId) {  
  31.         Log.i("LocalService""Received start id " + startId + ": " + intent);  
  32.   
  33.         // fetch the extra string from the alarm on/alarm off values  
  34.         String state = intent.getExtras().getString("extra");  
  35.         // fetch the whale choice integer values  
  36.         Integer whale_sound_choice = intent.getExtras().getInt("whale_choice");  
  37.   
  38.         Log.e("Ringtone extra is ", state);  
  39.         Log.e("Whale choice is ", whale_sound_choice.toString());  
  40.   
  41.         // put the notification here, test it out  
  42.   
  43.         // notification  
  44.         // set up the notification service  
  45.         NotificationManager notify_manager = (NotificationManager)  
  46.                 getSystemService(NOTIFICATION_SERVICE);  
  47.         // set up an intent that goes to the Main Activity  
  48.         Intent intent_main_activity = new Intent(this.getApplicationContext(), MainActivity.class);  
  49.         // set up a pending intent  
  50.         PendingIntent pending_intent_main_activity = PendingIntent.getActivity(this, 0,  
  51.                 intent_main_activity, 0);  
  52.   
  53.         // make the notification parameters  
  54.         Notification notification_popup = new Notification.Builder(this)  
  55.                 .setContentTitle("An alarm is going off!")  
  56.                 .setContentText("Click me!")  
  57.                 .setSmallIcon(R.drawable.ic_action_call)  
  58.                 .setContentIntent(pending_intent_main_activity)  
  59.                 .setAutoCancel(true)  
  60.                 .build();  
  61.   
  62.   
  63.   
  64.   
  65.         // this converts the extra strings from the intent  
  66.         // to start IDs, values 0 or 1  
  67.         assert state != null;  
  68.         switch (state) {  
  69.             case "alarm on":  
  70.                 startId = 1;  
  71.                 break;  
  72.             case "alarm off":  
  73.                 startId = 0;  
  74.                 Log.e("Start ID is ", state);  
  75.                 break;  
  76.             default:  
  77.                 startId = 0;  
  78.                 break;  
  79.         }  
  80.   
  81.   
  82.         // if else statements  
  83.   
  84.         // if there is no music playing, and the user pressed "alarm on"  
  85.         // music should start playing  
  86.         if (!this.isRunning && startId == 1) {  
  87.             Log.e("there is no music, ""and you want start");  
  88.   
  89.             this.isRunning = true;  
  90.             this.startId = 0;  
  91.   
  92.             // set up the start command for the notification  
  93.             notify_manager.notify(0, notification_popup);  
  94.   
  95.   
  96.   
  97.             // play the whale sound depending on the passed whale choice id  
  98.   
  99.             if (whale_sound_choice == 0) {  
  100.                 // play a randomly picked audio file  
  101.   
  102.                 int minimum_number = 1;  
  103.                 int maximum_number = 13;  
  104.   
  105.                 Random random_number = new Random();  
  106.                 int whale_number = random_number.nextInt(maximum_number + minimum_number);  
  107.                 Log.e("random number is " , String.valueOf(whale_number));  
  108.   
  109.   
  110.                 if (whale_number == 1) {  
  111.                     media_song = MediaPlayer.create(this, R.raw.humpback_bubblenet_and_vocals);  
  112.                     media_song.start();  
  113.                 }  
  114.                 else if (whale_number == 2) {  
  115.                     // create an instance of the media player  
  116.                     media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_moo);  
  117.                     // start the ringtone  
  118.                     media_song.start();  
  119.                 }  
  120.                 else if (whale_number == 3) {  
  121.                     media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_whup);  
  122.                     media_song.start();  
  123.                 }  
  124.                 else if (whale_number == 4) {  
  125.                     media_song = MediaPlayer.create(this, R.raw.humpback_feeding_call);  
  126.                     media_song.start();  
  127.                 }  
  128.                 else if (whale_number == 5) {  
  129.                     media_song = MediaPlayer.create(this, R.raw.humpback_flipper_splash);  
  130.                     media_song.start();  
  131.                 }  
  132.                 else if (whale_number == 6) {  
  133.                     media_song = MediaPlayer.create(this, R.raw.humpback_tail_slaps_with_propeller_whine);  
  134.                     media_song.start();  
  135.                 }  
  136.                 else if (whale_number == 7) {  
  137.                     media_song = MediaPlayer.create(this, R.raw.humpback_whale_song);  
  138.                     media_song.start();  
  139.                 }  
  140.                 else if (whale_number == 8) {  
  141.                     media_song = MediaPlayer.create(this, R.raw.humpback_whale_song_with_outboard_engine_noise);  
  142.                     media_song.start();  
  143.                 }  
  144.                 else if (whale_number == 9) {  
  145.                     media_song = MediaPlayer.create(this, R.raw.humpback_wheeze_blows);  
  146.                     media_song.start();  
  147.                 }  
  148.                 else if (whale_number == 10) {  
  149.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_echolocation_clicks);  
  150.                     media_song.start();  
  151.                 }  
  152.                 else if (whale_number == 11) {  
  153.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_offshore);  
  154.                     media_song.start();  
  155.                 }  
  156.                 else if (whale_number == 12) {  
  157.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  158.                     media_song.start();  
  159.                 }  
  160.                 else if (whale_number == 13) {  
  161.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_transient);  
  162.                     media_song.start();  
  163.                 }  
  164.                 else {  
  165.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  166.                     media_song.start();  
  167.                 }  
  168.   
  169.   
  170.             }  
  171.             else if (whale_sound_choice == 1) {  
  172.                 // create an instance of the media player  
  173.                 media_song = MediaPlayer.create(this, R.raw.humpback_bubblenet_and_vocals);  
  174.                 // start the ringtone  
  175.                 media_song.start();  
  176.             }  
  177.             else if (whale_sound_choice == 2) {  
  178.                 // create an instance of the media player  
  179.                 media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_moo);  
  180.                 // start the ringtone  
  181.                 media_song.start();  
  182.             }  
  183.             else if (whale_sound_choice == 3) {  
  184.                 media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_whup);  
  185.                 media_song.start();  
  186.             }  
  187.             else if (whale_sound_choice == 4) {  
  188.                 media_song = MediaPlayer.create(this, R.raw.humpback_feeding_call);  
  189.                 media_song.start();  
  190.             }  
  191.             else if (whale_sound_choice == 5) {  
  192.                 media_song = MediaPlayer.create(this, R.raw.humpback_flipper_splash);  
  193.                 media_song.start();  
  194.             }  
  195.             else if (whale_sound_choice == 6) {  
  196.                 media_song = MediaPlayer.create(this, R.raw.humpback_tail_slaps_with_propeller_whine);  
  197.                 media_song.start();  
  198.             }  
  199.             else if (whale_sound_choice == 7) {  
  200.                 media_song = MediaPlayer.create(this, R.raw.humpback_whale_song);  
  201.                 media_song.start();  
  202.             }  
  203.             else if (whale_sound_choice == 8) {  
  204.                 media_song = MediaPlayer.create(this, R.raw.humpback_whale_song_with_outboard_engine_noise);  
  205.                 media_song.start();  
  206.             }  
  207.             else if (whale_sound_choice == 9) {  
  208.                 media_song = MediaPlayer.create(this, R.raw.humpback_wheeze_blows);  
  209.                 media_song.start();  
  210.             }  
  211.             else if (whale_sound_choice == 10) {  
  212.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_echolocation_clicks);  
  213.                 media_song.start();  
  214.             }  
  215.             else if (whale_sound_choice == 11) {  
  216.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_offshore);  
  217.                 media_song.start();  
  218.             }  
  219.             else if (whale_sound_choice == 12) {  
  220.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  221.                 media_song.start();  
  222.             }  
  223.             else if (whale_sound_choice == 13) {  
  224.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_transient);  
  225.                 media_song.start();  
  226.             }  
  227.             else {  
  228.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  229.                 media_song.start();  
  230.             }  
  231.   
  232.   
  233.   
  234.   
  235.   
  236.   
  237.   
  238.   
  239.   
  240.   
  241.         }  
  242.   
  243.         // if there is music playing, and the user pressed "alarm off"  
  244.         // music should stop playing  
  245.         else if (this.isRunning && startId == 0) {  
  246.             Log.e("there is music, ""and you want end");  
  247.   
  248.             // stop the ringtone  
  249.             media_song.stop();  
  250.             media_song.reset();  
  251.   
  252.             this.isRunning = false;  
  253.             this.startId = 0;  
  254.         }  
  255.   
  256.         // these are if the user presses random buttons  
  257.         // just to bug-proof the app  
  258.         // if there is no music playing, and the user pressed "alarm off"  
  259.         // do nothing  
  260.         else if (!this.isRunning && startId == 0) {  
  261.             Log.e("there is no music, ""and you want end");  
  262.   
  263.             this.isRunning = false;  
  264.             this.startId = 0;  
  265.   
  266.         }  
  267.   
  268.         // if there is music playing and the user pressed "alarm on"  
  269.         // do nothing  
  270.         else if (this.isRunning && startId == 1) {  
  271.             Log.e("there is music, ""and you want start");  
  272.   
  273.             this.isRunning = true;  
  274.             this.startId = 1;  
  275.   
  276.         }  
  277.   
  278.         // can't think of anything else, just to catch the odd event  
  279.         else {  
  280.             Log.e("else ""somehow you reached this");  
  281.   
  282.         }  
  283.       return START_NOT_STICKY;  
  284.     }  
  285.   
  286.     @Override  
  287.     public void onDestroy() {  
  288.         // Tell the user we stopped.  
  289.         Log.e("on Destroy called""ta da");  
  290.   
  291.         super.onDestroy();  
  292.         this.isRunning = false;  
  293.     }  
  294.   
  295. }  

Step 11

Create new dimens.xml file into values folder (File ⇒ New ⇒Activity⇒Empty_activity).

Go to dimens.xml then click the text bottom. This xml file contains the designing code for android app. Into the dimens.xml copy and paste the below code

dimens.xml code

  1. <resources>  
  2.     <!-- Default screen margins, per the Android Design guidelines. -->  
  3.     <dimen name="activity_horizontal_margin">16dp</dimen>  
  4.     <dimen name="activity_vertical_margin">16dp</dimen>  
  5.     <dimen name="fab_margin">16dp</dimen>  
  6. </resources>  

Step 12

Create one folder into the Resources folder. Paste your set of ringtones into the folder.

Step 13

Create new strings.xml file into Values folder (File ⇒ New ⇒Activity⇒Empty_activity).

Go to strings.xml and click the text botton. This XML file contains the designing code for Android app. Into the strings.xml, copy and paste the below code.

strings.xml code 

  1. <resources>  
  2.     <string name="app_name">Alarm Clock</string>  
  3.     <string name="action_settings">Settings</string>  
  4.   
  5.     <string-array name="whale_array">  
  6.         <item>Pick a whale sound!</item>  
  7.         <item>Humpback bubble net</item>  
  8.         <item>Humpback contact Call Moo</item>  
  9.         <item>Humpback contact Call Whup</item>  
  10.         <item>Humpback feeding Call</item>  
  11.         <item>Humpback flipper splash</item>  
  12.         <item>Humpback Tail slaps</item>  
  13.         <item>Humpback whale song</item>  
  14.         <item>Humpback whale song with outboard engine</item>  
  15.         <item>Humpback wheeze blows</item>  
  16.         <item>Killer whale echolocation clicks</item>  
  17.         <item>Killer whale offshore</item>  
  18.         <item>Killer whale resident</item>  
  19.         <item>Killer whale transient</item>  
  20.     </string-array>  
  21.   
  22. </resources>  

Step 14

Click the Make Project option and run.

Android

Deliverables

Here, we have successfully created Alarm Android application.

Android
Android
Android
Android

Android

Don’t forgot to like and follow me. If you have any doubt, just commend below.