Jim
Jim

Reputation: 9234

Android: notifications

In my app, I need to show notification every day at 9.00 am, 3.00 pm, 8.00 pm. How to do that...should I use alarm Manager ? Should I create 3 alarm managers or one can do this things for me ? What is the simpliest way ? Any tutorial ? Thanks

Upvotes: 0

Views: 254

Answers (2)

rizalp1
rizalp1

Reputation: 6524

Use one alarm manager.

Something like this (just rough code)

int duration = 0; // seconds
int now = getTwentyFourHrTime();
if (now == 9) { duration = 6*60*60; }
if (now == 15) { duration = 5 * 60 * 60 ; }
if (now == 20) { duration = 13 * 60 * 60 ; }

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
 Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, duration);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);

Upvotes: 0

Khantahr
Khantahr

Reputation: 8528

I'd set an alarm for 9am using the AlarmManager. Then when the BroadcastReceiver handles the alarm, I'd have it set a new alarm for 3pm. When that BroadcastReceiver is triggered, have it set one for 8pm, and so on.

Upvotes: 1

Related Questions