静音Google登录Android后台服务 [英] Silent Google Sign In in android background service

查看:75
本文介绍了静音Google登录Android后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的Android应用程序中运行后台服务.我使用从登录活动中获得的IdToken在后端服务器上进行身份验证.该服务以START_STICKY模式运行,因此即使关闭该应用程序,该服务仍在后台运行,以从后端服务器获取任何通知.我面临的问题是,当IdToken过期时,我无法在服务本身中续订它.如果令牌已过期,则回调函数不会收到任何结果.如果令牌尚未过期,它将立即获得结果.

I am running a background service in my android app. I use the IdToken that I get from the sign in activity to authenticate at the backend server. The service is running in START_STICKY mode, so even when the app is closed, the service keeps running in the background to get any notifications from the backend server. The problem I'm facing is when the IdToken expires, I am not able to renew it in the service itself. The callback function does not receive any result if the token has expired. If the token has not expired yet, it gets the result instantaneously.

这是signIn函数和handleSignIn函数的代码.

Here is the code for the signIn function and handleSignIn function.

private void signIn() {
        new Thread(new Runnable() { public void run() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestIdToken("<My server_client_id>")
                .build();
        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn
                (mGoogleApiClient);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done"

            // and the GoogleSignInResult will be available instantly.
            Log.d(TAG, "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            // If the user has not previously signed in on this device or the sign-in has
            // expired,
            // this asynchronous branch will attempt to sign in the user silently.  Cross-device
            // single sign-on will occur in this branch.
            Log.d(TAG, "had to sign in again");
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    Log.d(TAG, "got result");
                    handleSignInResult(googleSignInResult);
                }
            });
        }}}).start();
    }

推荐答案

遇到了同样的问题,所以这是我到目前为止的发现.

Got the same issue, so here are my findings so far.

在令牌过期之前,登录过程实际上并未使用Google API客户端.

Before the token expires, the sign-in process does not actually use the Google API client.

如果令牌已过期,则将使用Google API客户端,并且在调用silentSignIn()时需要将其连接.您可以使用blockingConnect()手动连接它,也可以使用enableAutoManage()自动连接它(如果您处于活动状态).

If the token is expired, the Google API client is used and it needs to be connected when calling silentSignIn(). You can connect it manually with blockingConnect(), or automatically with enableAutoManage() (if you are in an activity).

因此,在您的情况下,看来您至少应该连接客户端.

So in your case it looks like you should at least connect the client.

但是还有更多...如果ID令牌已过期,则登录过程将涉及接收活动结果,您只能在活动中进行此操作,而不能在服务中进行.

But there's more... If the ID token is expired, the sign-in process involves receiving an activity result, which you can only do in an activity, not a service.

这篇关于静音Google登录Android后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆