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

michaelbahr
  • 4,362
  • 2
  • 31
  • 66
  • Have you seen this? http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/index.html#Alarm – user931366 Apr 02 '12 at 21:09

1 Answers1

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

Dan
  • 325
  • 3
  • 12
  • well one thing for people (like me) hitting into this post ~ is that it uses a deprecated approach: `onCreateDialog` within an [Activity](http://developer.android.com/reference/android/app/Activity.html) these days they use a `DialogFragment` lined out in this [dialog guide](http://developer.android.com/guide/topics/ui/controls/pickers.html) – pellekrogholt Sep 05 '14 at 08:56
  • 5
    One person's "deprecated" is another's "works on older devices". – Edward Falk Jul 06 '15 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.. – androCoder-BD Jun 03 '16 at 19:51