1

I know there is a lot of discussion about AlarmClock and AlarmManager and how to set alarms, launch apps, etc. However, I am yet to find the perfect answer for the following scenario.

In my Android alarm clock application I'm developing, I want to set an alarm for a specific time, pass in other settings such as what song to play, vibrate, etc.

1) what should I use to do this? AlarmClock or AlarmManager?

2) Now, when the alarm goes off at the specified time, how do I tell it to launch my custom Activity? Again the question of AlarmClock or AlarmManager.

This custom activity would show the time and have buttons saying "Sleep" or "Snooze" which the user can press (pretty much what happens when an alarm in any other alarm clock app goes off). I don't want to launch my alarm app, ONLY that one screen when the alarm goes off. When the user hits a button, I want to close that custom acitvity and for nothing else related to that app to open. I would like it so that the user can go back to doing whatever they were doing.

Michael
  • 3,093
  • 7
  • 39
  • 83

1 Answers1

0

I think you can do your job with AlarmManager. As you say, if you want to set alarm for a specific time, you should use setRepeating(). If you want to wake up your device when the alarm fires choose RTC_WAKEUP type and if you don't want that RTC type. You define a PendingIntent, whatever you want to do(play sound, light and vibrate), you can do within this activity that attached to this intent. You should design your layout in that(YOUR_ACTIVITY) layout and put buttons to snooze/cancel alarm.

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, YOUR_ACTIVITY.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

Above code set an alarm for 14:00 and interval of one day which means it will be fired everyday 14:00 and run YOUR_ACTIVITY until you cancel it. this is an example in Google Android Documents.

Mehran Zamani
  • 831
  • 9
  • 31