0

I require to add a date in my code using DatePicker to set an Alarm, and the question is , is it possible to add a DatePicker along with Timepicker and set the alarm without causing trouble in earlier versions?

Here is the code I use :

public void clicker (View view)
{
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(AddAlarm.this,
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker,
                                      int selectedHour, int selectedMinute) {
                    Calendar calendar = Calendar.getInstance();
                    //calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
                    calendar.set(Calendar.MINUTE, selectedMinute);

                    int c_hour,c_min;
                    String format;
                    c_hour=selectedHour;
                    c_min=selectedMinute;

                    if (c_hour == 0) {
                        c_hour += 12;
                        format = "AM";
                    } else if (c_hour == 12) {
                        format = "PM";
                    } else if (c_hour > 12) {
                        c_hour -= 12;
                        format = "PM";
                    } else {
                        format = "AM";
                    }

                    String formatTime = String.format("%02d : %02d %s", c_hour, c_min, format);

                    TextView dimple = (TextView)findViewById(R.id.timeText);
                    dimple.setText(formatTime);
                    setAlarm(calendar);

                }
            }, hour, minute, false);// Yes 24 hour time
    mTimePicker.setTitle("Select Time");
    mTimePicker.show();



}

private void setAlarm(Calendar targetCal)
{
    Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
    alarmintent.putExtra("ALARM_NO", tempx);
    alarmintent.setAction("my.action.string");
    PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);


}
OBX
  • 6,044
  • 7
  • 33
  • 77

0 Answers0