Pulkit Tyagi
Pulkit Tyagi

Reputation: 61

How to Set AlarmManager For notification that has to be repeated multiple times a day and everyday

I am able to set AlarmManager for alarm that is triggered every 24 Hours :

    Calendar c= Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR_OF_DAY,holder.hours);
    c.set(Calendar.MINUTE,holder.min);
    Intent in=new Intent(Reminder_entry.this,Notificationservice.class);
    in.putExtra("name",holder.name);
    PendingIntent pi=PendingIntent.getService(Reminder_entry.this, holder.pi,      in,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManageram=AlarmManager)Reminder_entry.this.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 1000 * 60 * 60 *24,pi);

But i am not able to set it so it can repeat say every 6 hours a day and everyday . My research says i will have to daisyChain alarms so if one goes off i have to set for next day . Can you help me understand how this can be done? how can i reset the alarm when it is triggered and my pending intent is processed because my pending intent is calling A service and i dont know how to Set Alarm in a service .

this is my service :

public class Notificationservice extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  String name=intent.getStringExtra("name");
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent i=new Intent(Notificationservice.this,Notification_landing.class);
    PendingIntent pi=PendingIntent.getActivity(Notificationservice.this, 0, i,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder b= new NotificationCompat.Builder(Notificationservice.this);
    b.setAutoCancel(true).setContentTitle(name).setContentText("time to take "+name).setSmallIcon(R.drawable.ic_launcher).setSound(soundUri);
    b.setContentIntent(pi);
    Notification n=b.build();
    NotificationManager nm=(NotificationManager)Notificationservice.this.getSystemService(NOTIFICATION_SERVICE);

    nm.notify(1,n);
    return super.onStartCommand(intent, flags, startId);
}}

Upvotes: 0

Views: 1357

Answers (1)

aoemerson
aoemerson

Reputation: 147

I have recently implemented a service with an alarm, too, but won't claim to be an expert so more experienced users may disagree with my approach.

In my service, I do most of the real "work" in the onHandleIntent() method which is overridden from IntentService

In your case, I think it's a matter of doing the work then setting another alarm for 6 hours time when you want it to run again. For example:

     @Override
     protected void onHandleIntent(Intent intent) {
        // do the work that needs to be done every 6 hours
        someWork();

       // Now create a new PendingIntent and associate it with an alarm for 6 hours time
       // This example uses the current Intent and replays it but you could
       // modify it if you need to perform something different next time.

       PendingIntent pIntent = PendingIntent.getService(this, 1234, intent, 
                PendingIntent.FLAG_CANCEL_CURRENT);

       AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

       // Find out system time in milliseconds for 6 hours in the future
       long scheduledTime = System.currentTimeMillis() + (6 * 60 * 60 * 1000);

       // Set the alarm. Note that this version is using RTC - there are other options,
       // read the docs for more info: 
       // developer.android.com/reference/android/app/AlarmManager.html

       am.set(AlarmManager.RTC, scheduledTime, pIntent);
    }

Note that I opt not to use a Calendar for working out the future time for the alarm - I think it's less awkward just to use the System time in milliseconds.

Update - some additional comments

  1. From your code, I see you are extending from Service - I think it is easier to extend from IntentService as Android has implemented a lot more of the key functionality for you so that overriding onHandleIntent(Intent) is more or less all you need to do.
  2. My answer above uses AlarmManager rather than NotificationManager but the principle of daisy-chaining remains the same, I think.

Upvotes: 2

Related Questions