Google 在服务中进行身份验证

Google auth in a service

我正在开发一项服务来与游戏流程的活动和碎片进行通信。

但我的问题是,如果我通过服务调用 mGoogleApiClient.connect() 时,如何显示 google 在 activity 中自动显示的对话框?

这是我的服务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());
}
...

可以吗?

服务无法显示对话框,因为它们不是 UI 组件。也不能开始活动

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?

与其担心如何在服务中做到这一点,不如在 activity 的上下文中正确地做到这一点。有一些选项。

  • 将服务绑定到 activity 并让它们传达授权信息。 Activity 将处理授权部分(可能根据服务的请求)并将信息发送到服务。
  • 在启动服务之前让 activity 完成身份验证部分,并将身份验证信息传递给它。
  • 从服务中,显示一条通知,显示需要身份验证,点击它后,将用户带到 activity 进行身份验证,并将结果发送到服务。

可能还有更多方法可以解决这个问题,但我认为您理解这个想法。