Android - Simple Alarm Application

Submitted by: 
Operating System: 
Visitors have accessed this post 5653 times.

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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplealarmapplication.MainActivity">
  11.  
  12.  
  13. <TextView
  14. android:id="@+id/tv_display"
  15. android:layout_height="wrap_content"
  16. android:layout_width="wrap_content"
  17. android:text="Alarm not set"
  18. android:textSize="20sp"
  19. android:layout_marginTop="20dp"
  20. android:layout_centerHorizontal="true"/>
  21.  
  22. <TimePicker
  23. android:layout_below="@+id/tv_display"
  24. android:id="@+id/tp_time"
  25. android:layout_height="wrap_content"
  26. android:layout_width="match_parent" />
  27.  
  28. android:id="@+id/btn_set"
  29. android:layout_below="@+id/tp_time"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="Set Alarm"/>
  33.  
  34. android:id="@+id/btn_reset"
  35. android:layout_below="@+id/btn_set"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:text="Reset Alarm"/>
  39. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplealarmapplication">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22.  
  23. <receiver android:name=".AlarmAdapter"
  24. android:enabled="true"
  25. android:exported="true" />
  26. </application>
  27. </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!!!


Comments

Very nice! Simple solution that I sought for

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.