I am currently working on adding the Google API Activity Recognition.
I succeeded in getting the Activities reported using a pending intent as shown in the sample at: http://developer.android.com/training/location/activity-recognition.html
Now, I would like a callback from the IntentService (fired by the pending intent). To do this I tried the solution found at: Using ResultReceiver in Android
Intent intent = new Intent(ctx, ActivityRecognitionIntentService.class);
intent.putExtra(ActivityRecognitionIntentService.REQUEST_RECEIVER_EXTRA, new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case ActivityRecognitionIntentService.RESULT_ID_WITH_ACTIVITYRESULT:
ActivityRecognitionResult result = resultData.getParcelable(ActivityRecognitionIntentService.RESULT_BUNDLE_ACTIVITYRESULT);
DetectedActivity mostProbableActivity = result.getMostProbableActivity();
observer.onNext(mostProbableActivity);
// Get the confidence percentage for the most probable activity
int confidence = mostProbableActivity.getConfidence();
// Get the type of activity
int activityType = mostProbableActivity.getType();
Log.d(TAG, getNameFromType(activityType) + " confidence: " + confidence + " isMoving: " + isMoving(activityType));
break;
case ActivityRecognitionIntentService.RESULT_ID_NO_ACTIVITYRESULT:
Log.e(TAG, "Nonfatal: no activity result");
break;
default:
Log.e(TAG, "Unexpected resultCode: " + resultCode);
}
}
});
This unfortunately resulted in ActivityRecognitionResult.hasResult(intent)
always returning false.
/**
* Called when a new activity detection update is available.
*/
@Override
protected void onHandleIntent(Intent intent) {
// outputs "onHandleIntent false"
Log.d(TAG, "onHandleIntent " + ActivityRecognitionResult.hasResult(intent));
}
Further testing showed that any extras passed to the intent caused the same problem, so for example:
Intent intent = new Intent(ctx, ActivityRecognitionIntentService.class);
intent.putExtra("TEST", "TESTING");
PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, 0, pendingIntent);
Makes yet again ActivityRecognitionResult.hasResult(intent)
return false. Removing the "TEST" extra, gives the Activity results as expected.
So finally my questions:
I found a solution using a LocalBroadcastReceiver from within the IntentService, forwarding the results.
The implementation can be found in: https://github.com/mcharmas/Android-ReactiveLocation