Google auth in a service

时间:2017-08-04 13:00:18

标签: android google-play-services google-play-games

Im developing a service to communicate with the activities and fragments the game flow.

But my problem is, how can I show the dialogs that google automatically display in the activity when I call mGoogleApiClient.connect() , if Im calling it over a service?

This is my service class:

public class MultiplayerService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

public GoogleApiClient mGoogleApiClient;

@Override
public void onCreate() {
    super.onCreate();
    mBus.register(this);
    initGoogleClient();
}

...

@Override
public void onConnected(@Nullable Bundle bundle) {
    mEventPoster.postEventSafely(new BusEvent.UserLogged());
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    mEventPoster.postEventSafely(new BusEvent.LogInFailed());
}

@Subscribe
public void onLogginClicked(BusEventActivity.LogInClicked event) {
    mGoogleApiClient.connect();
    mEventPoster.postEventSafely(new BusEvent.UserLogged());
}
...

Is it possible?

1 个答案:

答案 0 :(得分:1)

Services are not capable of showing dialogs, since they are not UI components. Neither can they start activities.

how can I show the dialogs that google automatically display in the activity when I call mGoogleApiClient.connect() , if Im calling it over a service?

Instead of worrying about how to do it in a service, do it properly, in the context an activity. There are some options.

  • Bind the service to an activity and have them communicate the auth information. Activity will take care of the auth part (perhaps on request of the service) and send the info to the service.
  • Have an activity do the auth part before starting the service, and pass the auth info to it.
  • From the service, show a notification displaying that auth is required, upon tapping it, take the user to an activity to auth, and send the result to the service.

There might be more ways to go about it, but I think you understand the idea.