Android: Start Service with Context.startService vs PendingIntent.getService

12,561

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.

Share:
12,561
Metro Smurf
Author by

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, 2022

Comments

  • Metro Smurf
    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

    1. When would you start a service with Context.startService vs a PendingIntent?
    2. Why would you use one over the other?
  • Metro Smurf
    Metro Smurf over 12 years
    So, is there ever a time when you would want to start a service with a PendingIntent?
  • JoxTraex
    JoxTraex over 12 years
    If 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
    Lior over 6 years
    Just notice that you have to add your service in manifest and add export="true" property its important