Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I call startIntentSenderForResult in Service properly?

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.

like image 717
khris Avatar asked May 14 '14 07:05

khris


1 Answers

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.

like image 64
matiash Avatar answered Nov 16 '22 01:11

matiash