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);
}
}
就是這樣,享受與延遲和其他變量打太...