Wednesday 20 January 2016

How to set Alarm in Android programmatically ?


To set an alarm in android you have use AlarmManager class. Following is the code to set alarm.

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
In the above example cal is object of Calendar.
you can instantiate it as following :
Calendar cal=Calendar.getInstance(); cal.set(Calendar.MONTH,5); cal.set(Calendar.YEAR,2016); cal.set(Calendar.DAY_OF_MONTH,29); cal.set(Calendar.HOUR_OF_DAY,17); cal.set(Calendar.MINUTE,30);
 There is little thing to mention here this alarm will fire on 29 june 2016 because the months are zero-based. 

following Is the code of class AlarmSetter 


import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class AlarmSetter extends Activity {
   Button btn;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.yourlayout);
   Calendar cal=Calendar.getInstance();
   cal.set(Calendar.MONTH,5);
   cal.set(Calendar.YEAR,2011);
   cal.set(Calendar.DAY_OF_MONTH,29);
   cal.set(Calendar.HOUR_OF_DAY,17);
   cal.set(Calendar.MINUTE,30);

  
    Intent intent = new Intent(this, Mote.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
    Toast.makeText(this, "Alarm Set.", Toast.LENGTH_LONG).show();
}
You have to to declare the class AlarmSetter in the Android Manifest file.
In the above example We are declaring PendingIntent.
In the PendingIntent.getBroadcast(this.getApplicationContext(), 
1253,intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

In this there are four parameters 

1) Application Context
2) Unique id
3) intent
4) Type of BroadcastReciever.


You do need to much worry about Application context,
Unique id (choose any random id),Type of BroadcastReciever
but the third parameter is important. In the Intent you will 
specify the BroadcastReciever which will trigger when the 
alarm will fire. We have declared Mote which is subclass 
of BroadcastReciever  
Following is the Source Code of Mote class

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Mote extends BroadcastReceiver{

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
  
}



}

Do not forget to Declare Mote BroadcastReceiver in Manifest 
<receiver  android:name=".Mote"></receiver>

Thanks
For any query feel free to contact me at nitin_sharma639@yahoo.com

  

8 comments:

  1. Hi,
    I want just to set the alarm and do nothing (like you invoke onReceive when the alarm goes off). What should be the pending Intent parameter in the set method then. I set it to null but the alarm is not being set. Is the pending intent necessary for setting alarm.

    ReplyDelete
    Replies
    1. Hi pending intent is important to receive the intent in BroadcastReceiver whenever the alarm goes off

      Thanks

      Delete
  2. hi
    i want to set tomorrow reminder in this code ....give me code.

    ReplyDelete
  3. Why is MOte.class necessary? Its justs creating a toast. TIA

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hi prince Mote.class is for handling the intent whenever the alarm goes of. You can pass parameter while setting the alarm like intent.putStringExtra() and receive the same parameters in intent received in onRecieve method method . Hope it help you.

      Delete
  4. Hello , Excuse me, in my app i have seekbar and some codes to change the brightness... I wish to change it with the alarm (When the alarm fired , the brightness changes..) , but since the method OnReceive in another class , how could i do it?

    ReplyDelete
    Replies
    1. Hello This is not relevant question to the tutorial. You can put this question on forums. You can use LocalBroadCastManager for this purpose. Thanks

      Delete