gpt4 book ai didi

android - 用户登录后重新运行/继续 SyncAdapter

转载 作者:行者123 更新时间:2023-11-30 02:54:12 26 4
gpt4 key购买 nike

如果在 SyncAdapter.onPerformSync 中固有的身份验证 token 获取导致用户需要输入密码,一旦他们成功登录,您如何重新运行或继续同步?

虽然这似乎是一个相当标准的功能,但我一直找不到它的任何内容。 AccountManager.getAuthToken docs似乎暗示使用 an OnAccountsUpdatedListener ,但我已经尝试过了,但它从未被调用过。此外,如果我对文档的理解正确,将要求对任何帐户进行任何更改,这似乎有点...间接。

编辑

这是我的代码的相关部分。我密切关注 official guide这个优秀的blog post by Udinic

在 Authenticator.getAuthToken 中

// There is neither an access nor a refresh token. User has to log in
if (!tokens.hasAccess() && !tokens.hasRefresh()) {
Intent intent = new Intent(context, LoginActivity.class);
intent.putExtra(LoginActivity.ARG_ACCOUNT, account);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); // As per AbstractAccountAuthenticator doc
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
}
...
return bundle;

在LoginActivity.finishLogin

// Store access token if one was provided. Note that this should always be the case anyway,
// otherwise the login would have been unsuccessful
if (tokens.hasAccess()) {
manager.setAuthToken(account, Authenticator.TOKEN_TYPE_ACCESS, tokens.access);
}
// Store refresh token if one was provided.
if (tokens.hasRefresh()) {
manager.setAuthToken(account, Authenticator.TOKEN_TYPE_REFRESH, tokens.refresh);
}
final Intent intent = new Intent();
if (tokens.hasAccess()) {
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);
intent.putExtra(AccountManager.KEY_AUTHTOKEN, tokens.access);
}

setAccountAuthenticatorResult(intent.getExtras());
setResult(Activity.RESULT_OK, intent);
finish();

而且,尽管它有些微不足道:在 SyncAdapter.onPerformSync 中:

String token = null;
try {
token = AccountManager.get(getContext()).blockingGetAuthToken(account, Authenticator.TOKEN_TYPE_ACCESS, true);
if (token == null) {
syncResult.stats.numAuthExceptions++;
return;
}
} catch (OperationCanceledException x) {
return;
} catch (AuthenticatorException x) {
syncResult.stats.numAuthExceptions++;
return;
} catch (IOException x) {
syncResult.stats.numIoExceptions++;
return;
}

最佳答案

经过一番讨论,我认为您需要的是:

ContentResolver.requestSync(account, Contract.AUTHORITY, new Bundle());

当需要用户交互来完成同步时,您的客户经理非常合理地导致 blockingGetAuthToken 立即返回 null。您只需要安排一下,当用户完成操作(验证自己)时,您就可以请求新的同步。上面的代码将完成它。

关于android - 用户登录后重新运行/继续 SyncAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23582763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com