Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I call startResolutionForResult from a Service?

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.

like image 566
nybblesAndBits Avatar asked Nov 04 '14 21:11

nybblesAndBits


2 Answers

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

like image 109
Clayton Wilkinson Avatar answered Nov 04 '22 18:11

Clayton Wilkinson


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

like image 43
Alexey Zakharchenko Avatar answered Nov 04 '22 19:11

Alexey Zakharchenko