set alarm on selected time and date

prakash_d22 picture prakash_d22 · Nov 25, 2011 · Viewed 9.7k times · Source

I am developing a taskmanager on Android 2.1. I want to set alarm for a task set by date from datepicker and time from time picker Help me with the code..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText next = (EditText) findViewById(R.id.editText1);
    final Button sub = (Button) findViewById(R.id.button1);
    final Button res = (Button) findViewById(R.id.button2);
    final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
    final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);

    sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View view) {

             int y=dp.getYear();
             int mo=dp.getMonth();
             int day=dp.getDayOfMonth();

            Time t = new Time();
                                    int h=t.getCurrentHour();
                                    int m=t.getCurrentMinutes();

    }

 private AlarmManager getSystemService(String alarmService) {

            // TODO Auto-generated method stub
            return null;
        }

    });

Answer

Zeev G picture Zeev G · Dec 18, 2012

Hey this is how to set alarm on android AlarmManager to spesific date (android alarmmanager set alarm on date) I have been searching all over for this. pay attention to the value of the month!!

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//cal.add(Calendar.SECOND, 10);

cal.set(Calendar.DATE,19);  //1-31
cal.set(Calendar.MONTH,Calendar.DECEMBER);  //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,2012);//year...

cal.set(Calendar.HOUR_OF_DAY, 16);  //HOUR
cal.set(Calendar.MINUTE, 39);       //MIN
cal.set(Calendar.SECOND, 10);       //SEC


// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, alarmAct.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);

//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);