Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use PendingIntent.getService() or getBroadcast with AlarmManager?

My app needs to grab some data from the web at a specific time each day. So I use an AlarmManager to schedule the task and that works ok.

But when looking at various examples there seems to be two ways to deal with the AlarmManager when it comes to the pending intent.

One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.

Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.

Can someone explain to me the difference between the two approaches so I can decide on which one to rely?

EDIT: One more question is where to acquire the wake lock when using getService()?

For example, when using a BroadcastReceiver I have the following line in onReceive():

WakeReminderIntentService.acquireStaticLock(context); 

How should I acquire the wake lock if I instead call the service directly like:

PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, OnAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); 

Should I simply acquire it from within the service instead?

like image 208
marlar Avatar asked Sep 05 '11 12:09

marlar


People also ask

What is PendingIntent getBroadcast?

getBroadcast(Context context, int requestCode, Intent intent, int flags) Retrieve a PendingIntent that will perform a broadcast, like calling Context. sendBroadcast() .

Why would you use a PendingIntent?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

What is request code PendingIntent?

1- requestCode is used to get the same pending intent later on (for cancelling etc) 2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent.

What is PendingIntent Flag_update_current?

Setting PendingIntent. FLAG_UPDATE_CURRENT : Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.


2 Answers

One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.

it has one more step in starting service than

Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.

then you should use the second approach as it is reducing your one step in execution..

like image 180
Vineet Shukla Avatar answered Oct 18 '22 20:10

Vineet Shukla


Reading your edit I presume you found out yourself: If you want to make sure that your service is started when using AlarmManager, you better take the detour of first sending to a receiver and acquiring a wake lock there. Otherwise it is possible that the phone will sleep before the requested service is launched. That's what the javadoc of AlarmManager says and I also read it in post by Google engineer.

So now for your edit: When to acquire the lock? The whole point of using the receiver is to acquire the lock within the onReceive() method of the receiver, because Android will not fall asleep during the execution of this method.

For an example see this question.

like image 25
schnatterer Avatar answered Oct 18 '22 18:10

schnatterer