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
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. <android.support.design.widget.AppBarLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:theme="@style/AppTheme.AppBarOverlay">
  13. <android.support.v7.widget.Toolbar
  14. android:id="@+id/toolbar"
  15. android:layout_width="match_parent"
  16. android:layout_height="?attr/actionBarSize"
  17. android:background="?attr/colorPrimary"
  18. app:popupTheme="@style/AppTheme.PopupOverlay" />
  19. </android.support.design.widget.AppBarLayout>
  20. <include layout="@layout/content_main" />
  21. </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. <android.support.design.widget.AppBarLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:theme="@style/AppTheme.AppBarOverlay">
  13. <android.support.v7.widget.Toolbar
  14. android:id="@+id/toolbar"
  15. android:layout_width="match_parent"
  16. android:layout_height="?attr/actionBarSize"
  17. android:background="?attr/colorPrimary"
  18. app:popupTheme="@style/AppTheme.PopupOverlay" />
  19. </android.support.design.widget.AppBarLayout>
  20. <include layout="@layout/content_main" />
  21. </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. import android.annotation.TargetApi;
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.support.v7.widget.Toolbar;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.Button;
  18. import android.widget.Spinner;
  19. import android.widget.TextView;
  20. import android.widget.TimePicker;
  21. import java.util.Calendar;
  22. public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  23. //to make our alarm manager
  24. AlarmManager alarm_manager;
  25. TimePicker alarm_timepicker;
  26. TextView update_text;
  27. Context context;
  28. PendingIntent pending_intent;
  29. int choose_whale_sound;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  35. setSupportActionBar(toolbar);
  36. this.context = this;
  37. // initialize our alarm manager
  38. alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);
  39. //initialize our timepicker
  40. alarm_timepicker = (TimePicker) findViewById(R.id.timePicker);
  41. //initialize our text update box
  42. update_text = (TextView) findViewById(R.id.update_text);
  43. // create an instance of a calendar
  44. final Calendar calendar = Calendar.getInstance();
  45. // create an intent to the Alarm Receiver class
  46. final Intent my_intent = new Intent(this.context, Alarm_Receiver.class);
  47. // create the spinner in the main UI
  48. Spinner spinner = (Spinner) findViewById(R.id.richard_spinner);
  49. // Create an ArrayAdapter using the string array and a default spinner layout
  50. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  51. R.array.whale_array, android.R.layout.simple_spinner_item);
  52. // Specify the layout to use when the list of choices appears
  53. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  54. // Apply the adapter to the spinner
  55. spinner.setAdapter(adapter);
  56. // Set an onclick listener to the onItemSelected method
  57. spinner.setOnItemSelectedListener(this);
  58. // initialize start button
  59. Button alarm_on = (Button) findViewById(R.id.alarm_on);
  60. // create an onClick listener to start the alarm
  61. alarm_on.setOnClickListener(new View.OnClickListener() {
  62. @TargetApi(Build.VERSION_CODES.M)
  63. @Override
  64. public void onClick(View v) {
  65. // setting calendar instance with the hour and minute that we picked
  66. // on the time picker
  67. calendar.set(Calendar.HOUR_OF_DAY, alarm_timepicker.getHour());
  68. calendar.set(Calendar.MINUTE, alarm_timepicker.getMinute());
  69. // get the int values of the hour and minute
  70. int hour = alarm_timepicker.getHour();
  71. int minute = alarm_timepicker.getMinute();
  72. // convert the int values to strings
  73. String hour_string = String.valueOf(hour);
  74. String minute_string = String.valueOf(minute);
  75. // convert 24-hour time to 12-hour time
  76. if (hour > 12) {
  77. hour_string = String.valueOf(hour - 12);
  78. }
  79. if (minute < 10) {
  80. //10:7 --> 10:07
  81. minute_string = "0" + String.valueOf(minute);
  82. }
  83. // method that changes the update text Textbox
  84. set_alarm_text("Alarm set to: " + hour_string + ":" + minute_string);
  85. // put in extra string into my_intent
  86. // tells the clock that you pressed the "alarm on" button
  87. my_intent.putExtra("extra", "alarm on");
  88. // put in an extra int into my_intent
  89. // tells the clock that you want a certain value from the drop-down menu/spinner
  90. my_intent.putExtra("whale_choice", choose_whale_sound);
  91. Log.e("The whale id is" , String.valueOf(choose_whale_sound));
  92. // create a pending intent that delays the intent
  93. // until the specified calendar time
  94. pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0,
  95. my_intent, PendingIntent.FLAG_UPDATE_CURRENT);
  96. // set the alarm manager
  97. alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
  98. pending_intent);
  99. }
  100. });
  101. // initialize the stop button
  102. Button alarm_off = (Button) findViewById(R.id.alarm_off);
  103. // create an onClick listener to stop the alarm or undo an alarm set
  104. alarm_off.setOnClickListener(new View.OnClickListener() {
  105. @Override
  106. public void onClick(View v) {
  107. // method that changes the update text Textbox
  108. set_alarm_text("Alarm off!");
  109. // cancel the alarm
  110. alarm_manager.cancel(pending_intent);
  111. // put extra string into my_intent
  112. // tells the clock that you pressed the "alarm off" button
  113. my_intent.putExtra("extra", "alarm off");
  114. // also put an extra int into the alarm off section
  115. // to prevent crashes in a Null Pointer Exception
  116. my_intent.putExtra("whale_choice", choose_whale_sound);
  117. // stop the ringtone
  118. sendBroadcast(my_intent);
  119. }
  120. });
  121. }
  122. private void set_alarm_text(String output) {
  123. update_text.setText(output);
  124. }
  125. @Override
  126. public boolean onCreateOptionsMenu(Menu menu) {
  127. // Inflate the menu; this adds items to the action bar if it is present.
  128. getMenuInflater().inflate(R.menu.menu_main, menu);
  129. return true;
  130. }
  131. @Override
  132. public boolean onOptionsItemSelected(MenuItem item) {
  133. // Handle action bar item clicks here. The action bar will
  134. // automatically handle clicks on the Home/Up button, so long
  135. // as you specify a parent activity in AndroidManifest.xml.
  136. int id = item.getItemId();
  137. //noinspection SimplifiableIfStatement
  138. if (id == R.id.action_settings) {
  139. return true;
  140. }
  141. return super.onOptionsItemSelected(item);
  142. }
  143. @Override
  144. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  145. // An item was selected. You can retrieve the selected item using
  146. // parent.getItemAtPosition(pos)
  147. // outputting whatever id the user has selected
  148. //Toast.makeText(parent.getContext(), "the spinner item is "
  149. // + id, Toast.LENGTH_SHORT).show();
  150. choose_whale_sound = (int) id;
  151. }
  152. @Override
  153. public void onNothingSelected(AdapterView<?> parent) {
  154. // Another interface callback
  155. }
  156. }
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. public class Alarm_Receiver extends BroadcastReceiver{
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. Log.e("We are in the receiver.", "Yay!");
  9. // fetch extra strings from the intent
  10. // tells the app whether the user pressed the alarm on button or the alarm off button
  11. String get_your_string = intent.getExtras().getString("extra");
  12. Log.e("What is the key? ", get_your_string);
  13. // fetch the extra longs from the intent
  14. // tells the app which value the user picked from the drop down menu/spinner
  15. Integer get_your_whale_choice = intent.getExtras().getInt("whale_choice");
  16. Log.e("The whale choice is ", get_your_whale_choice.toString());
  17. // create an intent to the ringtone service
  18. Intent service_intent = new Intent(context, RingtonePlayingService.class);
  19. // pass the extra string from Receiver to the Ringtone Playing Service
  20. service_intent.putExtra("extra", get_your_string);
  21. // pass the extra integer from the Receiver to the Ringtone Playing Service
  22. service_intent.putExtra("whale_choice", get_your_whale_choice);
  23. // start the ringtone service
  24. context.startService(service_intent);
  25. }
  26. }
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. import java.util.Random;
  13. public class RingtonePlayingService extends Service {
  14. MediaPlayer media_song;
  15. int startId;
  16. boolean isRunning;
  17. @Override
  18. public IBinder onBind(Intent intent) {
  19. return null;
  20. }
  21. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  22. @Override
  23. public int onStartCommand(Intent intent, int flags, int startId) {
  24. Log.i("LocalService", "Received start id " + startId + ": " + intent);
  25. // fetch the extra string from the alarm on/alarm off values
  26. String state = intent.getExtras().getString("extra");
  27. // fetch the whale choice integer values
  28. Integer whale_sound_choice = intent.getExtras().getInt("whale_choice");
  29. Log.e("Ringtone extra is ", state);
  30. Log.e("Whale choice is ", whale_sound_choice.toString());
  31. // put the notification here, test it out
  32. // notification
  33. // set up the notification service
  34. NotificationManager notify_manager = (NotificationManager)
  35. getSystemService(NOTIFICATION_SERVICE);
  36. // set up an intent that goes to the Main Activity
  37. Intent intent_main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
  38. // set up a pending intent
  39. PendingIntent pending_intent_main_activity = PendingIntent.getActivity(this, 0,
  40. intent_main_activity, 0);
  41. // make the notification parameters
  42. Notification notification_popup = new Notification.Builder(this)
  43. .setContentTitle("An alarm is going off!")
  44. .setContentText("Click me!")
  45. .setSmallIcon(R.drawable.ic_action_call)
  46. .setContentIntent(pending_intent_main_activity)
  47. .setAutoCancel(true)
  48. .build();
  49. // this converts the extra strings from the intent
  50. // to start IDs, values 0 or 1
  51. assert state != null;
  52. switch (state) {
  53. case "alarm on":
  54. startId = 1;
  55. break;
  56. case "alarm off":
  57. startId = 0;
  58. Log.e("Start ID is ", state);
  59. break;
  60. default:
  61. startId = 0;
  62. break;
  63. }
  64. // if else statements
  65. // if there is no music playing, and the user pressed "alarm on"
  66. // music should start playing
  67. if (!this.isRunning && startId == 1) {
  68. Log.e("there is no music, ", "and you want start");
  69. this.isRunning = true;
  70. this.startId = 0;
  71. // set up the start command for the notification
  72. notify_manager.notify(0, notification_popup);
  73. // play the whale sound depending on the passed whale choice id
  74. if (whale_sound_choice == 0) {
  75. // play a randomly picked audio file
  76. int minimum_number = 1;
  77. int maximum_number = 13;
  78. Random random_number = new Random();
  79. int whale_number = random_number.nextInt(maximum_number + minimum_number);
  80. Log.e("random number is " , String.valueOf(whale_number));
  81. if (whale_number == 1) {
  82. media_song = MediaPlayer.create(this, R.raw.humpback_bubblenet_and_vocals);
  83. media_song.start();
  84. }
  85. else if (whale_number == 2) {
  86. // create an instance of the media player
  87. media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_moo);
  88. // start the ringtone
  89. media_song.start();
  90. }
  91. else if (whale_number == 3) {
  92. media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_whup);
  93. media_song.start();
  94. }
  95. else if (whale_number == 4) {
  96. media_song = MediaPlayer.create(this, R.raw.humpback_feeding_call);
  97. media_song.start();
  98. }
  99. else if (whale_number == 5) {
  100. media_song = MediaPlayer.create(this, R.raw.humpback_flipper_splash);
  101. media_song.start();
  102. }
  103. else if (whale_number == 6) {
  104. media_song = MediaPlayer.create(this, R.raw.humpback_tail_slaps_with_propeller_whine);
  105. media_song.start();
  106. }
  107. else if (whale_number == 7) {
  108. media_song = MediaPlayer.create(this, R.raw.humpback_whale_song);
  109. media_song.start();
  110. }
  111. else if (whale_number == 8) {
  112. media_song = MediaPlayer.create(this, R.raw.humpback_whale_song_with_outboard_engine_noise);
  113. media_song.start();
  114. }
  115. else if (whale_number == 9) {
  116. media_song = MediaPlayer.create(this, R.raw.humpback_wheeze_blows);
  117. media_song.start();
  118. }
  119. else if (whale_number == 10) {
  120. media_song = MediaPlayer.create(this, R.raw.killerwhale_echolocation_clicks);
  121. media_song.start();
  122. }
  123. else if (whale_number == 11) {
  124. media_song = MediaPlayer.create(this, R.raw.killerwhale_offshore);
  125. media_song.start();
  126. }
  127. else if (whale_number == 12) {
  128. media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);
  129. media_song.start();
  130. }
  131. else if (whale_number == 13) {
  132. media_song = MediaPlayer.create(this, R.raw.killerwhale_transient);
  133. media_song.start();
  134. }
  135. else {
  136. media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);
  137. media_song.start();
  138. }
  139. }
  140. else if (whale_sound_choice == 1) {
  141. // create an instance of the media player
  142. media_song = MediaPlayer.create(this, R.raw.humpback_bubblenet_and_vocals);
  143. // start the ringtone
  144. media_song.start();
  145. }
  146. else if (whale_sound_choice == 2) {
  147. // create an instance of the media player
  148. media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_moo);
  149. // start the ringtone
  150. media_song.start();
  151. }
  152. else if (whale_sound_choice == 3) {
  153. media_song = MediaPlayer.create(this, R.raw.humpback_contact_call_whup);
  154. media_song.start();
  155. }
  156. else if (whale_sound_choice == 4) {
  157. media_song = MediaPlayer.create(this, R.raw.humpback_feeding_call);
  158. media_song.start();
  159. }
  160. else if (whale_sound_choice == 5) {
  161. media_song = MediaPlayer.create(this, R.raw.humpback_flipper_splash);
  162. media_song.start();
  163. }
  164. else if (whale_sound_choice == 6) {
  165. media_song = MediaPlayer.create(this, R.raw.humpback_tail_slaps_with_propeller_whine);
  166. media_song.start();
  167. }
  168. else if (whale_sound_choice == 7) {
  169. media_song = MediaPlayer.create(this, R.raw.humpback_whale_song);
  170. media_song.start();
  171. }
  172. else if (whale_sound_choice == 8) {
  173. media_song = MediaPlayer.create(this, R.raw.humpback_whale_song_with_outboard_engine_noise);
  174. media_song.start();
  175. }
  176. else if (whale_sound_choice == 9) {
  177. media_song = MediaPlayer.create(this, R.raw.humpback_wheeze_blows);
  178. media_song.start();
  179. }
  180. else if (whale_sound_choice == 10) {
  181. media_song = MediaPlayer.create(this, R.raw.killerwhale_echolocation_clicks);
  182. media_song.start();
  183. }
  184. else if (whale_sound_choice == 11) {
  185. media_song = MediaPlayer.create(this, R.raw.killerwhale_offshore);
  186. media_song.start();
  187. }
  188. else if (whale_sound_choice == 12) {
  189. media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);
  190. media_song.start();
  191. }
  192. else if (whale_sound_choice == 13) {
  193. media_song = MediaPlayer.create(this, R.raw.killerwhale_transient);
  194. media_song.start();
  195. }
  196. else {
  197. media_song = MediaPlayer.create(this, R.raw.killerwhale_resident);
  198. media_song.start();
  199. }
  200. }
  201. // if there is music playing, and the user pressed "alarm off"
  202. // music should stop playing
  203. else if (this.isRunning && startId == 0) {
  204. Log.e("there is music, ", "and you want end");
  205. // stop the ringtone
  206. media_song.stop();
  207. media_song.reset();
  208. this.isRunning = false;
  209. this.startId = 0;
  210. }
  211. // these are if the user presses random buttons
  212. // just to bug-proof the app
  213. // if there is no music playing, and the user pressed "alarm off"
  214. // do nothing
  215. else if (!this.isRunning && startId == 0) {
  216. Log.e("there is no music, ", "and you want end");
  217. this.isRunning = false;
  218. this.startId = 0;
  219. }
  220. // if there is music playing and the user pressed "alarm on"
  221. // do nothing
  222. else if (this.isRunning && startId == 1) {
  223. Log.e("there is music, ", "and you want start");
  224. this.isRunning = true;
  225. this.startId = 1;
  226. }
  227. // can't think of anything else, just to catch the odd event
  228. else {
  229. Log.e("else ", "somehow you reached this");
  230. }
  231. return START_NOT_STICKY;
  232. }
  233. @Override
  234. public void onDestroy() {
  235. // Tell the user we stopped.
  236. Log.e("on Destroy called", "ta da");
  237. super.onDestroy();
  238. this.isRunning = false;
  239. }
  240. }
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. <string-array name="whale_array">
  5. <item>Pick a whale sound!</item>
  6. <item>Humpback bubble net</item>
  7. <item>Humpback contact Call Moo</item>
  8. <item>Humpback contact Call Whup</item>
  9. <item>Humpback feeding Call</item>
  10. <item>Humpback flipper splash</item>
  11. <item>Humpback Tail slaps</item>
  12. <item>Humpback whale song</item>
  13. <item>Humpback whale song with outboard engine</item>
  14. <item>Humpback wheeze blows</item>
  15. <item>Killer whale echolocation clicks</item>
  16. <item>Killer whale offshore</item>
  17. <item>Killer whale resident</item>
  18. <item>Killer whale transient</item>
  19. </string-array>
  20. </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.