Android - Simple Alarm Application

View: 885    Dowload: 0   Comment: 0   Post by: daii   Category: Mobile applications   Fields: Game - Entertainment

Download  android-simple-alarm.zip (540.99 KB)

You need to Sign In to download the file android-simple-alarm.zip
If you do not have an account then Sign up for free here

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!!!

Android - Simple Alarm Application

Learn on how 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.

Posted on 07-05-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Flap the Bird

    Flap the Bird

    View: 6    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tap your screen to make the bird fly. While you are flying there are destruction that will stop you. Avoid hitting the floor and flying to high this will deduct your life. You can collect coins and life along the way. For more mobile games and...

  • Bad Santa

    Bad Santa

    View: 11    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This is a simple shooting game where the Bad Santa is shooting the bad snowman and reindeer. You can collect coins and life along the way. For more mobile games and tutorials you can visit this site.

  • Flappy Halloween Game

    Flappy Halloween Game

    View: 6    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Flappy Halloween is a simple game, tap the screen to move the player. Shoot the pumpkin in the ring to gain score. Avoid hitting the flying destructions and falling from the ground. Since the Halloween is coming this game is really perfect to play...

  • Tilt Ball Game

    Tilt Ball Game

    View: 6    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This game is simple you need to tilt your device to make the ball move in your own desired direction. Reach the finish line before the time ends and move to the next level.

  • Sky Rise 3D Game

    Sky Rise 3D Game

    View: 26    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Stack up the blocks as high as you can to build a tower. Align the blocks on top of each other. If you manage to place the block exactly on the one before it, it will not lose size. Sloppy placement on the other hand will cut off the blocks and...

  • Flappy Bird Game

    Flappy Bird Game

    View: 26    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    The game is very simple, you have to navigate the bird to past the obstacles. You need to tap the screen to make the bird flies. Don’t fly too high because it will die. This game is not 100% cloned from the original Flappy Bird game only the...

  • Motocross Dirt Racing Game

    Motocross Dirt Racing Game

    View: 14    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Motocross Dirt Racing is an action packed form of off road racing. Unlike the real motocross races, in this game there’s no risk of injury. Finish the race as fast as you can.This game is very exciting and challenging like the other extreme sports....

  • Android - Color Switcher Game

    Android - Color Switcher Game

    View: 9    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Color Switcher is a game that needs focus every time you are passing the obstacles. This game is fun and very challenging if you are looking for a game that is amusing then this game is perfect for you. If you touch the obstacle which has the...

 
File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd