2
我试图使用AccountAuthenticator和SyncAdapter指示帐户的身份验证/同步状态。我已经通过了样本,并能够正常工作。设置帐户同步指示器红色(或其他颜色)
如何将指标设置为红色,就像GMail帐户一样?
我还想在同步适配器页面上添加其他状态指示器。见下图:
我试图使用AccountAuthenticator和SyncAdapter指示帐户的身份验证/同步状态。我已经通过了样本,并能够正常工作。设置帐户同步指示器红色(或其他颜色)
如何将指标设置为红色,就像GMail帐户一样?
我还想在同步适配器页面上添加其他状态指示器。见下图:
回答我的问题对未来团队的知识......
获取指示灯变色后,被一些实验相当容易。首先创建一个基于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);
}
}
就是这样,享受与延迟和其他变量打太...