Reputation: 365
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.
Upvotes: 1
Views: 1794
Reputation: 2813
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
Upvotes: 1
Reputation: 1007554
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.
Upvotes: 0