T.Barrett T.Barrett - 1 year ago 77
Android Question

Using a service to request activity updates - how to process Intents

Have an application that I am refactoring to use a service instead of an IntentService to process Activity updates. Currently using requestActivityUpdates to use a method that collects the intent and sends it to a IntentService based class for processing.

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
mGoogleApiClient,
DETECTION_INTERVAL_IN_MILLISECONDS,
getActivityDetectionPendingIntent()
).setResultCallback(this);


AND

private PendingIntent getActivityDetectionPendingIntent() {
Intent intent = new Intent(getApplicationContext(), ActivityIntentService.class);
return PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}


Is there a way to process the pendingIntent directly in my service class without passing it off to the IntentService?

Answer Source

PendingIntent.getService() works for both IntentService and regular Service. Just replace

Intent intent = new Intent(getApplicationContext(), ActivityIntentService.class);

with

Intent intent = new Intent(getApplicationContext(), ActivityService.class);

(or whatever the name of your Service is).

Also see the documentation for PendingIntent.getService():

Retrieve a PendingIntent that will start a service, like calling Context.startService(). The start arguments given to the service will come from the extras of the Intent.

The intent that is received by IntentService.onHandleIntent(Intent intent) will in a regular Service be received by onStartCommand(Intent intent, int flags, int startId).