Skip to content

Instantly share code, notes, and snippets.

@sankarganesh
Created August 30, 2013 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarganesh/6389756 to your computer and use it in GitHub Desktop.
Save sankarganesh/6389756 to your computer and use it in GitHub Desktop.
Alarm Setter Class
package com.san.samples;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MyView extends Activity {
/** Called when the activity is first created. */
int month = 0;
int year = 0;
int day = 0;
int hour = 0;
int minutes = 0;
int second = 0;
int count = 0;
DatePicker pickerDate;
TimePicker pickerTime;
Button triggerAlaram;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pickerDate = (DatePicker) findViewById(R.id.pickerdate);
pickerTime = (TimePicker) findViewById(R.id.pickertime);
triggerAlaram = (Button) findViewById(R.id.triggeralarm);
Calendar now = Calendar.getInstance();
pickerDate.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Toast.makeText(getApplicationContext(),
"onDateChanged", Toast.LENGTH_SHORT).show();
}
});
pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
pickerTime.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(getApplicationContext(), "onTimeChanged",
Toast.LENGTH_SHORT).show();
}
});
triggerAlaram.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
month = pickerDate.getMonth();
year = pickerDate.getYear();
day = pickerDate.getDayOfMonth();
hour = pickerTime.getCurrentHour();
minutes = pickerTime.getCurrentMinute();
count += 1;
setAlarm(month, year, day, hour, minutes, count);
}
});
}
private void setAlarm(int dmonth, int dyear, int d_day, int dhour,
int dminutes, int id) {
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.MONTH, dmonth);
Calendar_Object.set(Calendar.YEAR, dyear);
Calendar_Object.set(Calendar.DAY_OF_MONTH, d_day);
Calendar_Object.set(Calendar.HOUR_OF_DAY, dhour);
Calendar_Object.set(Calendar.MINUTE, dminutes);
Calendar_Object.set(Calendar.SECOND, 0);
// MyView is my current Activity, and AlarmReceiver is the
// BoradCastReceiver
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
id, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
/*
* The following sets the Alarm in the specific time by getting the long
* value of the alarm date time which is in calendar object by calling
* the getTimeInMillis(). Since Alarm supports only long value , we're
* using this method.
*/
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),
pendingIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment