I'm working on a Service which gets a location and I'm using Google Play Services for it.

According to http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#hasResolution%28%29, if hasResolution() returns true, calling startResolutionForResult may resolve an error. But it needs an Activity as first parameter.

Of course ConnectionResult returns a PendingIntent by getResolution() but the Service doesn't have a startIntentSenderForResult() like an Activity.
As far as I know, there is no way to get a result back in a Service.

How can I get a result in the Service? Or is there another proper way?

Edit:

Google Play Service SDK provides GooglePlayServicesUtil.showErrorNotification for background tasks. Of course accepted answer is good solution.

Edit 2:

showErrorNotification is only for a return value of isGooglePlayServicesAvailable.

有帮助吗?

解决方案

I would proceed this way:

  1. Show a notification indicating that there is a problem doing task X (actually, interacting with Google Play Services, but you may say something else more specific to your app).

  2. For this notification, provide a PendingIntent that starts your activity. As part of the extras for this PendingIntent, pass the PendingIntent provided by ConnectionResult.getResolution(). PendingIntents are parcelable so this shouldn't pose a problem.

  3. In onCreate() for this Activity, obtain the original PendingIntent from the extras, then call startIntentSenderForResult() with it. This will automatically redirect the user to wherever Google Play Services needed him to go (possibly log-in?)

  4. Then, in onActivityResult(), finish the activity, having first notified your service (via an Intent) that the problem is resolved (or not). The transient activity will have been invisible to the user.

I admit this solution is theoretical, but it should work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top