Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...