首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用GoogleSignIn + DriveClient时如何执行后台同步任务

使用GoogleSignIn + DriveClient时如何执行后台同步任务
EN

Stack Overflow用户
提问于 2018-05-21 03:13:01
回答 1查看 145关注 0票数 0

我们的用户有机会登录,授权应用程序访问他们的Google Drive。我们在GoogleSignInDriveClient的基础上实现。(不是另一个被弃用的接口GoogleApiClient)

https://developers.google.com/drive/android/auth

在用户退出应用程序后,我们希望Google Drive同步过程仍然在后台无缝工作,而不需要进一步的用户/UI交互。(能够处理登录令牌过期/几天/几小时后无效的情况)

我想知道如何做到这一点。欢迎使用任何代码示例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-24 03:56:14

silentSignIn是解决方案的关键。当silentSignIn失败时,我们还需要处理边缘情况。这是真实世界的代码片段。

代码语言:javascript
复制
public static GoogleSignInClient buildGoogleSignInClient() {
    GoogleSignInOptions signInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_APPFOLDER)
                    .build();
    return GoogleSignIn.getClient(WeNoteApplication.instance(), signInOptions);
}

public static boolean silentSignInThenSync(SyncViewModel syncViewModel, boolean handleSignInRequired) {
    GoogleSignInClient googleSignInClient = buildGoogleSignInClient();

    Task<GoogleSignInAccount> task = googleSignInClient.silentSignIn();

    if (task.isSuccessful()) {
        syncViewModel.getSlientSignInSuccessLiveData().postValue(true);

        return sync(task.getResult(), syncViewModel);
    } else {

        try {
            Tasks.await(task);
            try {
                GoogleSignInAccount googleSignInAccount = task.getResult(ApiException.class);

                syncViewModel.getSlientSignInSuccessLiveData().postValue(true);

                return sync(googleSignInAccount, syncViewModel);
            } catch (ApiException e) {
                Log.e(TAG, "", e);

                if (handleSignInRequired) {
                    if (e.getStatusCode() == GoogleSignInStatusCodes.SIGN_IN_REQUIRED) {
                        syncViewModel.getSignInRequiredLiveData().postValue(true);
                        return false;
                    }
                }

                String message = WeNoteApplication.instance().getString(R.string.sync_with_google_drive_failed_template, e.getLocalizedMessage());
                syncViewModel.getMessageLiveData().postValue(message);
            }
        } catch (ExecutionException e) {
            Log.e(TAG, "", e);

            if (handleSignInRequired) {
                if (e.getCause() instanceof ApiException) {
                    if (((ApiException) e.getCause()).getStatusCode() == GoogleSignInStatusCodes.SIGN_IN_REQUIRED) {
                        syncViewModel.getSignInRequiredLiveData().postValue(true);
                        return false;
                    }
                }
            }

            String message = WeNoteApplication.instance().getString(R.string.sync_with_google_drive_failed_template, e.getLocalizedMessage());
            syncViewModel.getMessageLiveData().postValue(message);
        } catch (InterruptedException e) {
            Log.e(TAG, "", e);

            String message = WeNoteApplication.instance().getString(R.string.sync_with_google_drive_failed_template, e.getLocalizedMessage());
            syncViewModel.getMessageLiveData().postValue(message);
        }
    }

    return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50438523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档