Create Instant Alarm Android App Using Android Studio

Introduction

 
In this article, I will show you how to create an Instant Alarm Android App using an Android studio.
 
Requirements
Steps should be followed
 
Follow these steps to create an instant Alarm Android app. I have included the source code below.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
Alarm android App
 
Step 2
 
You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Alarm android App
 
Step 3
 
Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role to run the application.
 
Alarm android App
 
Step 4
 
Now, add the activity and click the "Next" button.
 
Alarm android App
 
Step 5
 
Add Activity name and click "Finish".
 
Alarm android App
 
Step 6
 
Go to activity_main.xml. This XML file contains the designing code for the Android app. 
 
Alarm android App
 
The XML code is given below.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/time"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginTop="28dp"  
  14.         android:ems="10"  
  15.         android:hint="Number of minutes"  
  16.         android:inputType="numberDecimal" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/button1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignRight="@+id/time"  
  23.         android:layout_below="@+id/time"  
  24.         android:layout_marginRight="60dp"  
  25.         android:layout_marginTop="120dp"  
  26.         android:text="Start" />  
  27.   
  28. </RelativeLayout>  
Step 7
 
Create a new java class file. Go to (Java ⇒ new ⇒Java class file).
 
Alarm android App
 
Add the below code.
  1. package abu.alarm;    
  2. import android.content.BroadcastReceiver;    
  3. import android.content.Context;    
  4. import android.content.Intent;    
  5. import android.media.MediaPlayer;    
  6. import android.widget.Toast;    
  7.         
  8. public class MyBroadcastReceiver extends BroadcastReceiver {    
  9.     MediaPlayer mp;    
  10.     @Override    
  11.     public void onReceive(Context context, Intent intent) {    
  12.         mp=MediaPlayer.create(context, R.raw.alrm   );    
  13.         mp.start();    
  14.         Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();    
  15.     }    
  16. }   
Step 8
 
Add Music in the Res folder. The below template shows how to add the image file. Go to the app and right-click. The “Show in Explorer” option  will appear. Click on the path and add the image in the main folder.
 
Alarm android App
 
Create a new folder, name it as “Raw”, and add the music (mp3) file.
 
Alarm android App
 
Step 9
 
We need to make Vibrate requests, so add Vibrate permission in AndroidManifest.xml.
 
Alarm android App
 
The AndroidManifest.xml code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  3.  package="abu.alarm">    
  4.  <uses-permission android:name="android.permission.VIBRATE" />    
  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.                 <intent-filter>    
  14.                     <action android:name="android.intent.action.MAIN" />    
  15.         
  16.                     <category android:name="android.intent.category.LAUNCHER" />    
  17.                 </intent-filter>    
  18.             </activity>    
  19.             <receiver android:name="MyBroadcastReceiver" >    
  20.             </receiver>    
  21.         </application>    
  22. </manifest>   
Step 10
 
Go to Main Activity.java. This Java program is the backend language for Android.
 
Alarm android App
 
The Java code is given below.
  1. package abu.alarm;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlarmManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.   
  14. public class MainActivity extends Activity {  
  15.  Button b1;  
  16.   
  17.  @Override  
  18.  protected void onCreate(Bundle savedInstanceState) {  
  19.   super.onCreate(savedInstanceState);  
  20.   setContentView(R.layout.activity_main);  
  21.   b1 = (Button) findViewById(R.id.button1);  
  22.   
  23.   b1.setOnClickListener(new OnClickListener() {  
  24.   
  25.    @Override  
  26.    public void onClick(View v) {  
  27.     // TODO Auto-generated method stub    
  28.     startAlert();  
  29.    }  
  30.   });  
  31.   
  32.  }  
  33.  public void startAlert() {  
  34.   EditText text = (EditText) findViewById(R.id.time);  
  35.   int i = Integer.parseInt(text.getText().toString());  
  36.   Intent intent = new Intent(this, MyBroadcastReceiver.class);  
  37.   PendingIntent pendingIntent = PendingIntent.getBroadcast(  
  38.    this.getApplicationContext(), 234324243, intent, 0);  
  39.   AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
  40.   alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +  
  41.    (i * 1000), pendingIntent);  
  42.   Toast.makeText(this"Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();  
  43.  }  
  44.   

Step 11
 
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the error.
 
Alarm android App
 
Step 12
 
Then, click the Run button or press shift+f10 to run the project. And choose the virtual machine and click OK.
 
Alarm android App
 

Conclusion

 
We have successfully created an Instant Alarm Android application using Android Studio. 
 
Alarm android App
 
Alarm android App
 
Alarm android App


Similar Articles