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