Create Alarm Android Application

Introduction

 
Android is one of the most popular operating systems for mobiles. The alarm app takes place in day by day Android usage. Each Android mobile contains an alarm application. Simply alarm is our wake-up assistant. So, I will show you how to create an 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 an Alarm Android Application using Android Studio. I have included the source code below.
 
Step 1
 
Open the Android Studio and start a new project.
 
Android Studio
 
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 Studio
 
Step 3
 
Select the Android minimum SDK. After you chose the minimum SDK, it will show an approximate percentage of people who use that SDK. Click Next.
 
Android Studio
 
Step 4
 
Choose the basic activity, then click Next.
 
Android Studio
 
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 Studio
 
Step 6
 
Go to activity_main.xml then click the text bottom. This xml file contains the designing code for the 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 a 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 the 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, the 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.         // initialize the stop button  
  129.         Button alarm_off = (Button) findViewById(R.id.alarm_off);  
  130.         // create an onClick listener to stop the alarm or undo an alarm set  
  131.   
  132.         alarm_off.setOnClickListener(new View.OnClickListener() {  
  133.             @Override  
  134.             public void onClick(View v) {  
  135.   
  136.                 // method that changes the update text Textbox  
  137.                 set_alarm_text("Alarm off!");  
  138.   
  139.                 // cancel the alarm  
  140.                 alarm_manager.cancel(pending_intent);  
  141.   
  142.                 // put extra string into my_intent  
  143.                 // tells the clock that you pressed the "alarm off" button  
  144.                 my_intent.putExtra("extra""alarm off");  
  145.                 // also put an extra int into the alarm off section  
  146.                 // to prevent crashes in a Null Pointer Exception  
  147.                 my_intent.putExtra("whale_choice", choose_whale_sound);  
  148.   
  149.   
  150.                 // stop the ringtone  
  151.                 sendBroadcast(my_intent);
  152.             }  
  153.         });
  154.     }
  155.     private void set_alarm_text(String output) {  
  156.         update_text.setText(output);  
  157.     }
  158.     @Override  
  159.     public boolean onCreateOptionsMenu(Menu menu) {  
  160.         // Inflate the menu; this adds items to the action bar if it is present.  
  161.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  162.         return true;  
  163.     }  
  164.   
  165.     @Override  
  166.     public boolean onOptionsItemSelected(MenuItem item) {  
  167.         // Handle action bar item clicks here. The action bar will  
  168.         // automatically handle clicks on the Home/Up button, so long  
  169.         // as you specify a parent activity in AndroidManifest.xml.  
  170.         int id = item.getItemId();  
  171.   
  172.         //noinspection SimplifiableIfStatement  
  173.         if (id == R.id.action_settings) {  
  174.             return true;  
  175.         }  
  176.   
  177.         return super.onOptionsItemSelected(item);  
  178.     }  
  179.   
  180.     @Override  
  181.     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
  182.         // An item was selected. You can retrieve the selected item using  
  183.         // parent.getItemAtPosition(pos)  
  184.   
  185.         // outputting whatever id the user has selected  
  186.         //Toast.makeText(parent.getContext(), "the spinner item is "  
  187.         //        + id, Toast.LENGTH_SHORT).show();  
  188.         choose_whale_sound = (int) id;  
  189.   
  190.   
  191.     }  
  192.   
  193.     @Override  
  194.     public void onNothingSelected(AdapterView<?> parent) {  
  195.         // Another interface callback  
  196.   
  197.     }  
  198. }  
Step 9
 
Create a 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, the 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. public class Alarm_Receiver  extends BroadcastReceiver{  
  7.   
  8.   
  9.     @Override  
  10.     public void onReceive(Context context, Intent intent) {  
  11.   
  12.         Log.e("We are in the receiver.""Yay!");  
  13.   
  14.         // fetch extra strings from the intent  
  15.         // tells the app whether the user pressed the alarm on button or the alarm off button  
  16.         String get_your_string = intent.getExtras().getString("extra");  
  17.   
  18.         Log.e("What is the key? ", get_your_string);  
  19.   
  20.         // fetch the extra longs from the intent  
  21.         // tells the app which value the user picked from the drop down menu/spinner  
  22.         Integer get_your_whale_choice = intent.getExtras().getInt("whale_choice");  
  23.   
  24.         Log.e("The whale choice is ", get_your_whale_choice.toString());  
  25.   
  26.         // create an intent to the ringtone service  
  27.         Intent service_intent = new Intent(context, RingtonePlayingService.class);  
  28.   
  29.         // pass the extra string from Receiver to the Ringtone Playing Service  
  30.         service_intent.putExtra("extra", get_your_string);  
  31.         // pass the extra integer from the Receiver to the Ringtone Playing Service  
  32.         service_intent.putExtra("whale_choice", get_your_whale_choice);  
  33.   
  34.         // start the ringtone service  
  35.         context.startService(service_intent);
  36.     }
  37. }  
Step 10 
 
Create a 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, the 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.     @Override  
  22.     public IBinder onBind(Intent intent) {  
  23.         return null;  
  24.     }  
  25.   
  26.     @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
  27.     @Override  
  28.     public int onStartCommand(Intent intent, int flags, int startId) {  
  29.         Log.i("LocalService""Received start id " + startId + ": " + intent);  
  30.   
  31.         // fetch the extra string from the alarm on/alarm off values  
  32.         String state = intent.getExtras().getString("extra");  
  33.         // fetch the whale choice integer values  
  34.         Integer whale_sound_choice = intent.getExtras().getInt("whale_choice");  
  35.   
  36.         Log.e("Ringtone extra is ", state);  
  37.         Log.e("Whale choice is ", whale_sound_choice.toString());  
  38.   
  39.         // put the notification here, test it out  
  40.   
  41.         // notification  
  42.         // set up the notification service  
  43.         NotificationManager notify_manager = (NotificationManager)  
  44.                 getSystemService(NOTIFICATION_SERVICE);  
  45.         // set up an intent that goes to the Main Activity  
  46.         Intent intent_main_activity = new Intent(this.getApplicationContext(), MainActivity.class);  
  47.         // set up a pending intent  
  48.         PendingIntent pending_intent_main_activity = PendingIntent.getActivity(this, 0,  
  49.                 intent_main_activity, 0);  
  50.   
  51.         // make the notification parameters  
  52.         Notification notification_popup = new Notification.Builder(this)  
  53.                 .setContentTitle("An alarm is going off!")  
  54.                 .setContentText("Click me!")  
  55.                 .setSmallIcon(R.drawable.ic_action_call)  
  56.                 .setContentIntent(pending_intent_main_activity)  
  57.                 .setAutoCancel(true)  
  58.                 .build();  
  59.         // this converts the extra strings from the intent  
  60.         // to start IDs, values 0 or 1  
  61.         assert state != null;  
  62.         switch (state) {  
  63.             case "alarm on":  
  64.                 startId = 1;  
  65.                 break;  
  66.             case "alarm off":  
  67.                 startId = 0;  
  68.                 Log.e("Start ID is ", state);  
  69.                 break;  
  70.             default:  
  71.                 startId = 0;  
  72.                 break;  
  73.         }
  74.         // if else statements  
  75.   
  76.         // if there is no music playing, and the user pressed "alarm on"  
  77.         // music should start playing  
  78.         if (!this.isRunning && startId == 1) {  
  79.             Log.e("there is no music, ""and you want start");  
  80.   
  81.             this.isRunning = true;  
  82.             this.startId = 0;  
  83.   
  84.             // set up the start command for the notification  
  85.             notify_manager.notify(0, notification_popup);
  86.             // play the whale sound depending on the passed whale choice id
  87.             if (whale_sound_choice == 0) {  
  88.                 // play a randomly picked audio file  
  89.   
  90.                 int minimum_number = 1;  
  91.                 int maximum_number = 13;  
  92.   
  93.                 Random random_number = new Random();  
  94.                 int whale_number = random_number.nextInt(maximum_number + minimum_number);  
  95.                 Log.e("random number is " , String.valueOf(whale_number));
  96.                 if (whale_number == 1) {  
  97.                     media_song = MediaPlayer.create(this, R.raw.humpback_bubblenet_and_vocals);  
  98.                     media_song.start();  
  99.                 }  
  100.                 else if (whale_number == 2) {  
  101.                     // create an instance of the media player  
  102.                     media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_moo);  
  103.                     // start the ringtone  
  104.                     media_song.start();  
  105.                 }  
  106.                 else if (whale_number == 3) {  
  107.                     media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_whup);  
  108.                     media_song.start();  
  109.                 }  
  110.                 else if (whale_number == 4) {  
  111.                     media_song = MediaPlayer.create(this, R.raw.humpback_feeding_call);  
  112.                     media_song.start();  
  113.                 }  
  114.                 else if (whale_number == 5) {  
  115.                     media_song = MediaPlayer.create(this, R.raw.humpback_flipper_splash);  
  116.                     media_song.start();  
  117.                 }  
  118.                 else if (whale_number == 6) {  
  119.                     media_song = MediaPlayer.create(this, R.raw.humpback_tail_slaps_with_propeller_whine);  
  120.                     media_song.start();  
  121.                 }  
  122.                 else if (whale_number == 7) {  
  123.                     media_song = MediaPlayer.create(this, R.raw.humpback_whale_song);  
  124.                     media_song.start();  
  125.                 }  
  126.                 else if (whale_number == 8) {  
  127.                     media_song = MediaPlayer.create(this, R.raw.humpback_whale_song_with_outboard_engine_noise);  
  128.                     media_song.start();  
  129.                 }  
  130.                 else if (whale_number == 9) {  
  131.                     media_song = MediaPlayer.create(this, R.raw.humpback_wheeze_blows);  
  132.                     media_song.start();  
  133.                 }  
  134.                 else if (whale_number == 10) {  
  135.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_echolocation_clicks);  
  136.                     media_song.start();  
  137.                 }  
  138.                 else if (whale_number == 11) {  
  139.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_offshore);  
  140.                     media_song.start();  
  141.                 }  
  142.                 else if (whale_number == 12) {  
  143.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  144.                     media_song.start();  
  145.                 }  
  146.                 else if (whale_number == 13) {  
  147.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_transient);  
  148.                     media_song.start();  
  149.                 }  
  150.                 else {  
  151.                     media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  152.                     media_song.start();  
  153.                 }
  154.             }  
  155.             else if (whale_sound_choice == 1) {  
  156.                 // create an instance of the media player  
  157.                 media_song = MediaPlayer.create(this, R.raw.humpback_bubblenet_and_vocals);  
  158.                 // start the ringtone  
  159.                 media_song.start();  
  160.             }  
  161.             else if (whale_sound_choice == 2) {  
  162.                 // create an instance of the media player  
  163.                 media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_moo);  
  164.                 // start the ringtone  
  165.                 media_song.start();  
  166.             }  
  167.             else if (whale_sound_choice == 3) {  
  168.                 media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_whup);  
  169.                 media_song.start();  
  170.             }  
  171.             else if (whale_sound_choice == 4) {  
  172.                 media_song = MediaPlayer.create(this, R.raw.humpback_feeding_call);  
  173.                 media_song.start();  
  174.             }  
  175.             else if (whale_sound_choice == 5) {  
  176.                 media_song = MediaPlayer.create(this, R.raw.humpback_flipper_splash);  
  177.                 media_song.start();  
  178.             }  
  179.             else if (whale_sound_choice == 6) {  
  180.                 media_song = MediaPlayer.create(this, R.raw.humpback_tail_slaps_with_propeller_whine);  
  181.                 media_song.start();  
  182.             }  
  183.             else if (whale_sound_choice == 7) {  
  184.                 media_song = MediaPlayer.create(this, R.raw.humpback_whale_song);  
  185.                 media_song.start();  
  186.             }  
  187.             else if (whale_sound_choice == 8) {  
  188.                 media_song = MediaPlayer.create(this, R.raw.humpback_whale_song_with_outboard_engine_noise);  
  189.                 media_song.start();  
  190.             }  
  191.             else if (whale_sound_choice == 9) {  
  192.                 media_song = MediaPlayer.create(this, R.raw.humpback_wheeze_blows);  
  193.                 media_song.start();  
  194.             }  
  195.             else if (whale_sound_choice == 10) {  
  196.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_echolocation_clicks);  
  197.                 media_song.start();  
  198.             }  
  199.             else if (whale_sound_choice == 11) {  
  200.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_offshore);  
  201.                 media_song.start();  
  202.             }  
  203.             else if (whale_sound_choice == 12) {  
  204.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  205.                 media_song.start();  
  206.             }  
  207.             else if (whale_sound_choice == 13) {  
  208.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_transient);  
  209.                 media_song.start();  
  210.             }  
  211.             else {  
  212.                 media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);  
  213.                 media_song.start();  
  214.             }
  215.         }  
  216.   
  217.         // if there is music playing, and the user pressed "alarm off"  
  218.         // music should stop playing  
  219.         else if (this.isRunning && startId == 0) {  
  220.             Log.e("there is music, ""and you want end");
  221.             // stop the ringtone  
  222.             media_song.stop();  
  223.             media_song.reset();  
  224.   
  225.             this.isRunning = false;  
  226.             this.startId = 0;  
  227.         }
  228.         // these are if the user presses random buttons  
  229.         // just to bug-proof the app  
  230.         // if there is no music playing, and the user pressed "alarm off"  
  231.         // do nothing  
  232.         else if (!this.isRunning && startId == 0) {  
  233.             Log.e("there is no music, ""and you want end");  
  234.   
  235.             this.isRunning = false;  
  236.             this.startId = 0;
  237.         }
  238.         // if there is music playing and the user pressed "alarm on"  
  239.         // do nothing  
  240.         else if (this.isRunning && startId == 1) {  
  241.             Log.e("there is music, ""and you want start");
  242.             this.isRunning = true;  
  243.             this.startId = 1;
  244.         }
  245.         // can't think of anything else, just to catch the odd event  
  246.         else {  
  247.             Log.e("else ""somehow you reached this");
  248.         }  
  249.       return START_NOT_STICKY;  
  250.     }  
  251.   
  252.     @Override  
  253.     public void onDestroy() {  
  254.         // Tell the user we stopped.  
  255.         Log.e("on Destroy called""ta da");
  256.         super.onDestroy();  
  257.         this.isRunning = false;  
  258.     }  
  259.   
  260. }  
Step 11
 
Create a new dimens.xml file into the values folder (File ⇒ New ⇒Activity⇒Empty_activity).
 
Go to dimens.xml then click the text bottom. This xml file contains the designing code for the 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 the Values folder (File ⇒ New ⇒Activity⇒Empty_activity).
 
Go to strings.xml and click the text button. This XML file contains the designing code for the 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 an Alarm Android application.
 
Alarm app in Android
 
Android
 
Don’t forget to like and follow me. If you have any doubt, just comment below.


Similar Articles