Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在应用程序启动(Android)之间缓存GoogleAccountCredential?_Android_Google Drive Api - Fatal编程技术网

如何在应用程序启动(Android)之间缓存GoogleAccountCredential?

如何在应用程序启动(Android)之间缓存GoogleAccountCredential?,android,google-drive-api,Android,Google Drive Api,我想让用户登录一次使用Google Drive SDK v2,而不是每次启动应用时都问他。既然GoogleAccountCredential没有实现Serializable和Parcelable将其保存在持久性存储中,我该怎么做?如何检查用户是否已登录 现在我必须要求用户在每次启动应用程序时登录: GoogleAccountCredential googleDriveCredential = GoogleAccountCredential.usingOAuth2(this, DriveScope

我想让用户登录一次使用Google Drive SDK v2,而不是每次启动应用时都问他。既然
GoogleAccountCredential
没有实现
Serializable
Parcelable
将其保存在持久性存储中,我该怎么做?如何检查用户是否已登录

现在我必须要求用户在每次启动应用程序时登录:

GoogleAccountCredential googleDriveCredential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE_APPDATA);
startActivityForResult(googleDriveCredential.newChooseAccountIntent(), ACTION_LINK_GOOGLE_DRIVE);
我更喜欢像在Dropbox Android SDK中那样进行:

DbxAccountManager accountManager = DropboxAccountManagerHolder.get().getAccountManager();
if (!accountManager.hasLinkedAccount())
    DropboxAccountManagerHolder.get().getAccountManager()
                    .startLink(this, ACTION_LINK_DROPBOX);

这似乎是自动完成的:。 问题是检查它是否已登录。为此,您必须获取凭证令牌并处理异常:

使用特殊的Android auth实用程序使用驱动器服务时: 如果用户已授权该应用程序,请使用驱动器服务 将获得授权并按预期工作。如果用户没有 授权使用驱动器服务的应用程序将抛出 UserRecoverableAuthException包含可用于 向用户显示“授权授予”对话框

try {
  GoogleAccountCredential credential =
      GoogleAccountCredential.usingOAuth2(mContext, DriveScopes.DRIVE_FILE);
  credential.setSelectedAccountName(mAccount.name);
  // Trying to get a token right away to see if we are authorized
  credential.getToken();