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

时间:2011-05-20 03:25:37

标签: android

我正在尝试使用AccountAuthenticator和SyncAdapter指示帐户的身份验证/同步状态。我已经浏览了样本,可以让它正常工作。

如何将指标设置为红色,就像GMail帐户一样?

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

1 个答案:

答案 0 :(得分:2)

回答我自己关于未来团队知识的问题......

经过一些实验,让指标改变颜色相当容易。首先根据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);
    }

}

就是这样,喜欢玩延迟和其他变量......