Skip to main content

Android - Simple Alarm Application

Operating System


In this tutorial we will try to create a Simple Alarm Application using Android. This simple application can be used to make a schedule of your meeting, birthday, etc, just to make on time with it. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 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. tools:context="com.razormist.simplealarmapplication.MainActivity">
  8.  
  9.  
  10. <TextView
  11. android:id="@+id/tv_display"
  12. android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:text="Alarm not set"
  15. android:textSize="20sp"
  16. android:layout_marginTop="20dp"
  17. android:layout_centerHorizontal="true"/>
  18.  
  19. <TimePicker
  20. android:layout_below="@+id/tv_display"
  21. android:id="@+id/tp_time"
  22. android:layout_height="wrap_content"
  23. android:layout_width="match_parent" />
  24.  
  25. android:id="@+id/btn_set"
  26. android:layout_below="@+id/tp_time"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="Set Alarm"/>
  30.  
  31. android:id="@+id/btn_reset"
  32. android:layout_below="@+id/btn_set"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="Reset Alarm"/>
  36. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplealarmapplication">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21.  
  22. <receiver android:name=".AlarmAdapter"
  23. android:enabled="true"
  24. android:exported="true" />
  25. </application>
  26. </manifest>

Creating the Alarm Adapter

This code contains the script for the alarm. This will try to receive the request from the context to trigger the sound for the alarm. To do that create a new java class called AlarmAdapter then write these block of codes inside of it.

  1. package com.razormist.simplealarmapplication;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.media.MediaPlayer;
  7. import android.provider.Settings;
  8.  
  9. /**
  10.  * Created by Arvin on 5/5/2018.
  11.  */
  12.  
  13. public class AlarmAdapter extends BroadcastReceiver {
  14.  
  15. @Override
  16. public void onReceive(Context context, Intent intent) {
  17. MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
  18. mediaPlayer.start();
  19. }
  20. }

The Main Function

This code contains the main function of the application. This code will set the alarm then call the receiver to start the alarm base on the time given. To start with first locate your MainActivity java file and open it, then write these variable inside the MainActivity class.

  1. TimePicker tp_time;
  2. TextView tv_display;
  3. Button btn_set, btn_reset;

Finally, initialize the require methods inside the onCreate method to run the application.

  1. tv_display = (TextView)findViewById(R.id.tv_display);
  2. tp_time = (TimePicker)findViewById(R.id.tp_time);
  3. btn_set = (Button)findViewById(R.id.btn_set);
  4. btn_reset = (Button)findViewById(R.id.btn_reset);
  5.  
  6. btn_set.setOnClickListener(new View.OnClickListener() {
  7. @Override
  8. public void onClick(View v) {
  9. Calendar calendar = Calendar.getInstance();
  10.  
  11. if(Build.VERSION.SDK_INT >= 23) {
  12.  
  13. calendar.set(
  14. calendar.get(Calendar.YEAR),
  15. calendar.get(Calendar.MONTH),
  16. calendar.get(Calendar.DAY_OF_MONTH),
  17. tp_time.getHour(),
  18. tp_time.getMinute(),
  19. 0
  20. );
  21.  
  22.  
  23.  
  24. }else{
  25. calendar.set(
  26. calendar.get(Calendar.YEAR),
  27. calendar.get(Calendar.MONTH),
  28. calendar.get(Calendar.DAY_OF_MONTH),
  29. tp_time.getCurrentHour(),
  30. tp_time.getCurrentMinute(),
  31. 0
  32. );
  33. }
  34.  
  35.  
  36. setAlarm(calendar.getTimeInMillis(), calendar);
  37. }
  38.  
  39.  
  40. private void setAlarm(long timeInMillis, Calendar c) {
  41. AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  42.  
  43. Intent intent = new Intent(MainActivity.this, AlarmAdapter.class);
  44.  
  45. PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
  46.  
  47. alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
  48.  
  49. Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT).show();
  50. int hour = c.get(Calendar.HOUR_OF_DAY);
  51. int minute = c.get(Calendar.MINUTE);
  52. int ampm = c.get(Calendar.AM_PM);
  53. String day = "";
  54. if(ampm == Calendar.AM){
  55. day = "AM";
  56. }else if(ampm == Calendar.PM){
  57. day = "PM";
  58. }
  59. String timeText = "Alarm set for: ";
  60. timeText += hour +": " + minute + " " + day;
  61. tv_display.setText(timeText);
  62.  
  63. }
  64.  
  65.  
  66. });
  67.  
  68.  
  69.  
  70.  
  71. btn_reset.setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74.  
  75. AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  76.  
  77. Intent intent = new Intent(MainActivity.this, AlarmAdapter.class);
  78.  
  79. PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
  80.  
  81. alarmManager.cancel(pendingIntent);
  82.  
  83. tv_display.setText("Alarm not set");
  84.  
  85. }
  86. });

Try to run the app and see if it worked.

There you have it we have created a Simple Alarm Application using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byKirari (not verified)on Thu, 11/14/2019 - 10:20

Very nice! Simple solution that I sought for

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.