I'm quite new to Android but already have some experience with Java itself. Now I'd like to set up an App that asks for time and date and then sets up an alarm clock. I already looked through the google apis and lots of other stuff, but either I don't understand it or it's outdated.
Can anyone help me to set up that alarm clock while explaining how it works?
Thanks :)
This is working code in version 10. You need to set up an intent to start a new instance of the AlarmClock. make sure to assign the constants EXTRA_HOUR and EXTRA_MINUTE to your own variables names or hard coded constants. In this example they are coded to user entered time taken from the Calendar (located in the java.util.Calendar).
Intent openNewAlarm = new Intent(AlarmClock.ACTION_SET_ALARM);
openNewAlarm.putExtra(AlarmClock.EXTRA_HOUR, hour_alarm);
openNewAlarm.putExtra(AlarmClock.EXTRA_MINUTES, minute_alarm);
startActivity(openNewAlarm);
this next section obtains the current time from the internal clock and returns it in a TimePicker Here the user can next enter a new time and return it to the Intent to set a new alarm.
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
hour_local = c.get(Calendar.HOUR_OF_DAY);
minute_local = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour_local, minute_local,
DateFormat.is24HourFormat(getActivity()));
}
To use a TimePicker create an inner static class, one that is inside of the Activity the calls it. Look at this http://developer.android.com/reference/android/widget/TimePicker.html