gpt4 book ai didi

android - 将帐户同步指示器设置为红色(或其他颜色)

转载 作者:行者123 更新时间:2023-11-29 00:46:30 25 4
gpt4 key购买 nike

我正在尝试使用 AccountAuthenticator 和 SyncAdapter 指示帐户的身份验证/同步状态。我已经完成了示例,并且可以正常工作。

如何像GMail账号一样设置指示灯为红色?

我还想在同步适配器页面上添加额外的状态指示器。见下图:

最佳答案

为 future 的团队知识回答我自己的问题...

经过一些实验后,让指示器改变颜色相当容易。首先根据SDK示例工程中提供的代码创建一个工程,修改如下:

1) 在 AuthenticationActivity 期间从服务器伪造初始登录。一旦通过初始检查,系统将开始其定期同步尝试。

/**
* Called when the authentication process completes (see attemptLogin()).
*/
public void onAuthenticationResult(boolean result) {
Log.i(TAG, "onAuthenticationResult(" + result + ")");
// Hide the progress dialog
hideProgress();
// Override the result, we don't care right now....
result = true;
if (result) {
if (!mConfirmCredentials) {
finishLogin();
} else {
finishConfirmCredentials(true);
}
} else {
Log.e(TAG, "onAuthenticationResult: failed to authenticate");
if (mRequestNewAccount) {
// "Please enter a valid username/password.
mMessage.setText(getText(R.string.login_activity_loginfail_text_both));
} else {
// "Please enter a valid password." (Used when the
// account is already in the database but the password
// doesn't work.)
mMessage.setText(getText(R.string.login_activity_loginfail_text_pwonly));
}
}
}

2) 修改SyncAdapter中的“onPerformSync()”方法。这里的关键是“syncResult.stats”字段。在修改的时候,我发现插入多个错误并没有得到我想要的效果。还注意到计数似乎没有在同步尝试中记录(即失败总是以零出现)。 “lifetimeSyncs”是一个静态变量,用于对同步尝试进行计数。这段修改后的代码会不断的在绿色和红色之间交替...

 @Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
List<User> users;
List<Status> statuses;
String authtoken = null;

try {
// use the account manager to request the credentials
authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, true );
// fetch updates from the sample service over the cloud
//users = NetworkUtilities.fetchFriendUpdates(account, authtoken, mLastUpdated);
// update the last synced date.
mLastUpdated = new Date();
// update platform contacts.
Log.d(TAG, "Calling contactManager's sync contacts");
//ContactManager.syncContacts(mContext, account.name, users);
// fetch and update status messages for all the synced users.
//statuses = NetworkUtilities.fetchFriendStatuses(account, authtoken);
//ContactManager.insertStatuses(mContext, account.name, statuses);

if (SyncAdapter.lifetimeSyncs-- <= 0 ){
//mAccountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authtoken);
syncResult.stats.numAuthExceptions++;
//syncResult.delayUntil = 60;
lifetimeSyncs = 5;
}


} catch (final AuthenticatorException e) {
syncResult.stats.numParseExceptions++;
Log.e(TAG, "AuthenticatorException", e);
} catch (final OperationCanceledException e) {
Log.e(TAG, "OperationCanceledExcetpion", e);
} catch (final IOException e) {
Log.e(TAG, "IOException", e);
Log.d(TAG, extras.toString());
syncResult.stats.numAuthExceptions++;
syncResult.delayUntil = 60;
//extras.putString(AccountManager.KEY_AUTH_FAILED_MESSAGE, "You're not registered");
} catch (final ParseException e) {
syncResult.stats.numParseExceptions++;
Log.e(TAG, "ParseException", e);
}

}

就是这样,享受延迟和其他变量的乐趣......

关于android - 将帐户同步指示器设置为红色(或其他颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067136/

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