13

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 :)

1

1 Answer 1

14

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

3
  • well one thing for people (like me) hitting into this post ~ is that it uses a deprecated approach: onCreateDialog within an Activity these days they use a DialogFragment lined out in this dialog guide Sep 5, 2014 at 8:56
  • 5
    One person's "deprecated" is another's "works on older devices". Jul 6, 2015 at 23:26
  • I have a problem.. I also want to set the label for the alarm clock. How can i do that. I have tried EXTRA_MESSAGE.. but that does not sets the label of course.. so how can I do that.. Jun 3, 2016 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.