What is alarm manager

Android alarm manager is used to set the alarm in your mobile. The Alarm manager can access the system alarm.

In my program i am going to create a alarm prpgram in which you can enter the time in second and click on button it will set the alarm acording to your given time in second and first it will display the message in toast and then mobile vibrate for 10 seconds.


Alarm Manager:.

In this section i am going to show the alarm with practical example.

Open the android studio and create a project and give the name of your project like AlarmDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop one Textview , one EditText and one Button from pallet on to the MainActvity.

Explore the app->java and then right click on MainActivity package a context menu will display and click on new and then take a class and give the name of class as Alarm.

Write the code look like this in the Alarm.java file.

package com.example.niitpc.optionmenudemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;


public class Alarm extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

       Toast.makeText(context,"Alarm start",Toast.LENGTH_LONG).show();
       Vibrator v=(Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
       v.vibrate(10000);



    }
}

       


Explanation:

BroadcastReceiver:Here we need to extends the BroadcastReceiver class for onReceive().

onReceive():This method is called by the MainActivity.java .

Toast.makeText(context,"Alarm start",Toast.LENGTH_LONG).show():This statement show the message Alarm start in toast.

Vibrator v=(Vibrator) context.getSystemService(context.VIBRATOR_SERVICE):Here we are initialized the v object of Vibrate by calling the getSystemService(). It is take the one argument like context.VIBRATOR_SERVICE.

v.vibrate(10000):Here we call the vibrate().It is taking millisecond as argument for vibrate.


Open the Android.Menifest.xml file and write code look like this.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.niitpc.optionmenudemo">
<uses-permission android:name="android.permission.VIBRATE">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>
        
        <receiver android:name="Alarm" >
   </application>

</manifest>
     
     

Explanation:

<uses-permission android:name="android.permission.VIBRATE">This tag give the permission for vibrate the mobile when you use in your code.

<receiver android:name="Alarm" >This tag receive the broadcast here you need to pass the java class in my case it is Alarm.

Open the MainActivity.java file and the code is look like this.

package com.example.niitpc.optionmenudemo;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button btnalarm;
EditText time;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       
    btnalarm=(Button)findViewById(R.id.button);
    time=(EditText)findViewById(R.id.editText3);

    btnalarm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    int time1=Integer.parseInt(time.getText().toString());
    Intent intent=new Intent(MainActivity.this,Alarm.class);
    PendingIntent pi=PendingIntent.getBroadcast(getApplicationContext(),0,intent,0);
    AlarmManager am=(AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+time1*1000,pi);
        }
     });
   }

}

     

Explanation:

Button btnalarm:This statement create the btnalarm object of Button.

EditText time:This statement create the time object of EditText.

btnalarm=(Button)findViewById(R.id.button):This statement initialized the btnalarm object.

time=(EditText)findViewById(R.id.editText3):This statement initialized the time object.

int time1=Integer.parseInt(time.getText().toString()):This statement take the input from user in second and assign the time1 varible after int conversion.

Intent intent=new Intent(MainActivity.this,Alarm.class):This statement create the intent object of Intent.

PendingIntent pi=PendingIntent.getBroadcast(getApplicationContext(),0,intent,0):This statement create the pi object of the PendingIntent.The PendingIntent take the 4 argument,first is context of current class,second as 0 ,third as intent,fourth as 0.

AlarmManager am=(AlarmManager) getSystemService(ALARM_SERVICE):This statement initialized the am object of AlarmManager.

am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+time1*1000,pi):This statement set the alarm.set() take the 3 argument ,first is AlarmManager.RTC_WAKEUP,second is current time in millisecond+user given time for alarm,third is pi object of PendingIndent.

Alarm Manager.

What is Alarm for video Click here

Smiley face                     Smiley face