1

I wanted to know How I can set Alarm for a particular time. For example I want to set alarm for morning 9am daily. I googled a lot but found only the way to set alarm for a given interval only. like after 2 hours or next day.

Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
Sohan Badaya
  • 365
  • 4
  • 15

2 Answers2

1

hope this code helps you

Calendar calendar = Calendar.getInstance();

//9 AM 
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, YourClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

you should create BroadcastReceiver to receive intent.

read the documentation for further details

Hassy31
  • 2,793
  • 3
  • 21
  • 37
0

I googled a lot but found only the way to set alarm for a given interval only. like after 2 hours or next day.

The second parameter to setRepeating() on AlarmManager is when you want the alarm to go off first. Set that to be 9am tomorrow using a Calendar object, and use an RTC or RTC_WAKEUP alarm.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491