I'm trying to add GPS tracking functionality to my app by writing a GPS tracking Service. I've been following the Android Developer materials on how to do this via Google Play Services, but I'm stuck on the onConnectionFailed
method. I'm trying to call startResolutionForResult
to let Google Play Services handle the error. However this method requires that an activity be passed in as the first parameter, and since I'm calling it from a Service I'm not really sure what I should do.
I assume that I'm going about this all wrong and there's a completely different way to handle this from a service.
The authentication process for Google Play Services needs an activity since it may need to display UI to the user. Since services don't have UI, you need to send a message to an activity to set up the connection, then the service can continue. There is a good answer demonstrating one way to do this here: Example: Communication between Activity and Service using Messaging
You can use Status.getResolution() and start Activity with it. For example:
In Service
PendingIntent pI = status.getResolution();
mGoogleApiClient.getContext().startActivity(new Intent(mGoogleApiClient.getContext(), SomeActivity.class)
.putExtra("resolution", pI).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
In Activity onCreate:
PendingIntent pI = (PendingIntent) (getIntent().getParcelableExtra("resolution"));
startIntentSenderForResult(pI.getIntentSender(),1,null,0,0,0);
also add onActivityResult(int requestCode, int resultCode, Intent data) to receive result of resolution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With