代码之家  ›  专栏  ›  技术社区  ›  Cheok Yan Cheng

如果使用GoogleSignin+DriveClient,如何执行后台同步任务

  •  0
  • Cheok Yan Cheng  · 技术社区  · 7 年前

    我们的用户有机会登录,授权应用程序访问他们的谷歌驱动器。我们的实施基于 GoogleSignIn DriveClient . (不是另一个已弃用的API GoogleApiClient )

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

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

    我能知道如何做到这一点吗?感谢您提供任何代码示例。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Cheok Yan Cheng    7 年前

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


    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;
    }
    
    推荐文章