Android如何从SyncAdapter执行任何ui操作?

时间:2013-11-22 15:30:56

标签: android synchronization

我已完成与syncAdapter相关的所有操作,但现在我遇到了一个小问题

验证令牌

2小时后,我的令牌刚过期,然后我需要向用户显示一个再次输入密码的对话框,以便他可以更新他的令牌。

AccountManager.get(getContext()).getAuthToken(account, LoginActivity.ACCOUNT_TYPE, null, false, new AccountManagerCallback<Bundle>() {

            @Override
            public void run(AccountManagerFuture<Bundle> arg0) {
                try {
                    arg0.getResult();
                } catch (OperationCanceledException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (AuthenticatorException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }, null);

我在onPerformSync上运行它,但这不是打开一个活动。

1 个答案:

答案 0 :(得分:0)

这样做有两个部分

1)在您需要的AbstractThreadedSyncAdapter实施重写onPerformSync方法中

  • AccountManager使用方法blockingGetAuthToken()
  • 获取身份验证码
  • 尝试使用authcode执行同步过程(即网络服务电话或您使用它的任何内容)
  • 如果上一步失败,因为authcode已过期(例如您的网络服务器返回某种authcode过期消息),那么您需要通过AccountManager使用方法invalidateAuthToken()
  • 使authcode无效

2)在AbstractAccountAuthenticator实施重写的getAuthToken()方法

  • 使用AccountManager检索用户上次提供的密码,并尝试使用这些凭据从您的网络服务获取新的身份验证码。
  • 如果上一步失败,则添加一个意图,将登录活动打开到从getAuthToken()方法返回的包中。 这将导致登录屏幕显示

实施例

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse oResponse, Account oAccount, String strAuthTokenType, Bundle options) 
          throws NetworkErrorException {

    // Validate the authentication type         
    if (!strAuthTokenType.equals("TODO: your auth token type URL here")) 
    {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
        return result;
    }

    // Try to get the password already stored in account manger, if there is one
    final AccountManager oAccountManager = AccountManager.get(moContext);
    final String strPassword = oAccountManager.getPassword(oAccount);
    if (strPassword != null) 
    {   
        // TODO: Call the authentication web service method to get a fresh authcode
        // Pass the strPassword and oAccount.name
        Boolean blnVerified = //TODO: were the username + password authenticated?
        String strNewAuthCode = //TODO: the new authcode returned by your authentication web service

        // If it worked then return the result
        if (blnVerified) 
        {
            final Bundle result = new Bundle();
            result.putString(AccountManager.KEY_ACCOUNT_NAME, oAccount.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, "TODO: your account type URI here");
            result.putString(AccountManager.KEY_AUTHTOKEN, strNewAuthCode);
            return result;
        }
    }

    // Password is missing or incorrect. Start the activity to ask user to provide the missing credentials.
    // Open a UI form to get the user to input their username and password again
    final Intent oIntent = new Intent(moContext, frmAccount_Auth.class);
    oIntent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, oResponse);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, oIntent);
    return bundle;
}