Android: Start Service with Context.startService vs PendingIntent.getService
Solution 1
There really is no difference.
Specifically the Context method is used to directly start it where as a PendingIntent is typically used with a notification to fire this intent when it is tapped, which is delayed until the user taps it (typically). However; you wouldn't typically send the PendingIntent directly because that is not what it is for.
A PendingIntent is an Intent that is pending, pending, meaning that its NOT supposed to happen now, but in the near future. Whereas with an Intent, it is sent at the very moment.
If a PendingIntent is not pending when it is used, then it is no longer a PendingIntent and it is infact an Intent. Defeating the purpose entirely.
Solution 2
PendinIntents are very much used for widgets. As the layout of a running widget doesn't "belong" to your code, but it is instead under control of the system, you can't assign directly click listeners to the interface elements. Instead you assign a PendingIntent to those elements (like buttons) so when the user touches them, the PendingIntent is "executed", something like:
// get the widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.id.widget_layout);
// prepare to listen the clicks on the refresh button
Intent active = new Intent(context, WidgetCode.UpdateService.class);
PendingIntent refreshPendingIntent = PendingIntent.getService(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.buttonWidgetRefresh, refreshPendingIntent);
// send the changes to the widget
AppWidgetManager.getInstance(context).updateAppWidget(appwidgetid, remoteViews);
In this case a button in the widget starts a service. Usually you put extra info in the intent, with putExtras(), so the service will get any needed information to do its job.
Metro Smurf
By Day: Senior application architect. By Night: Currently interested in the structural (aka, dynamic) typing of JavaScript vs static typing of C#. PC Master Race: Crysis, Batman, BioShock, Metro 2033/Last Light, Ryse: Sone of Rome, Portal, Half-Life. The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague. -- E.W.Dijkstra
Updated on June 06, 2022Comments
-
Metro Smurf about 2 years
Context.startService
Intent intent = new Intent(context, MyService.class); context.startService(intent);
PendingIntent.getService
Intent intent = new Intent(context, MyService.class); PendingIntent pi = PendingIntent.getService(context, 0, intent, 0); pi.send();
Questions- When would you start a service with Context.startService vs a PendingIntent?
- Why would you use one over the other?
-
Metro Smurf over 12 yearsSo, is there ever a time when you would want to start a service with a
PendingIntent
? -
JoxTraex over 12 yearsIf you want to start a service in the near future, this would be the ideal case. Say I have a notification that displays new update available for a user's account. Ideally there would be a pending intent that would establish a connect to a server and download this information. I want it done when the user has tapped the notification NOT immediately, this way I wait for the user's convenience, or if the user doesn't care they can cancel the notification and the next new update will react the same way.
-
Lior over 6 yearsJust notice that you have to add your service in manifest and add export="true" property its important