AccountAuthenticatorActivity简介

本文详细介绍了Android.accounts.AccountAuthenticatorActivity的基本实现原理及其在SampleSyncAdapter中的应用示例。AccountAuthenticatorActivity作为AbstractAccountAuthenticator的帮助类,用于处理用户输入的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android.accounts.AccountAuthenticatorActivity 是AbstractAccountAuthenticator的帮助类的一个基本实现。
    当 AbstractAccountAuthenticator 需要一个Activity来让用户输入一些数据的时候,我就需要新建一个指向某个Activity的Intent,并把传进来的 AccountAuthenticatorResponse  参数以  KEY_ACCOUNT_MANAGER_RESPONSE    为键放在Intent,然后把启动Activity的相关参数放入其中,最后把Intent以为键放在Bundle的,并返回该Bundle。 这样系统将启动我们所指定的Activity,在Activity中完成工作后通过 AccountAuthenticatorResponse onResult(Bundle)   or   onError(int, String)   返回结果 
    AccountAuthenticatorActivity 正是这种Activity的基本实现。AccountAuthenticatorActivity中可以通过来设置返回结果,这样在 AccountAuthenticatorActivity finish() 函数,他会调用 AccountAuthenticatorResponse onResult(Bundle)   or   onError(int, String)   返回结果
AccountAuthenticatorActivity 的源码很简单,如下:
public  class   AccountAuthenticatorActivity   extends  Activity {
    private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
    private Bundle mResultBundle = null;

    /**
     * Set the result that is to be sent as the result of the request that caused this
     * Activity to be launched. If result is null or this method is never called then
     * the request will be canceled.
     * @param result this is returned as the result of the AbstractAccountAuthenticator request
     */
    public final void setAccountAuthenticatorResult(Bundle result) {
        mResultBundle = result;
    }

    /**
     * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
     * icicle is non-zero.
     * @param icicle the save instance data of this Activity, may be null
     */
     protected void  onCreate (Bundle icicle) {
        super.onCreate(icicle);

        mAccountAuthenticatorResponse =
                getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);

         if  (mAccountAuthenticatorResponse != null) {
            mAccountAuthenticatorResponse.onRequestContinued();
        }
    }

    /**
     * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
     */
     public void   finish () {
         if  (mAccountAuthenticatorResponse != null) {
             // send the result bundle back if set, otherwise send an error.
             if  (mResultBundle != null) {
                mAccountAuthenticatorResponse.onResult(mResultBundle);
            }  else  {
                mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
                        "canceled");
            }
            mAccountAuthenticatorResponse = null;
        }
        super.finish();
    }
}
以下是 SampleSyncAdapter 中的 AccountAuthenticatorActivity 的一个实例的代码片段:
    /**
     * Called when response is received from the server for confirm credentials
     * request. See onAuthenticationResult(). Sets the
     * AccountAuthenticatorResult which is sent back to the caller.
     * 
     * @param the confirmCredentials result.
     */
     protected void   finishConfirmCredentials (boolean result) {
        Log.i(TAG, "finishConfirmCredentials()");
        final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
         mAccountManager.setPassword(account, mPassword);
        final Intent intent = new Intent();
         intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, result);
        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 
     * Called when response is received from the server for authentication
     * request. See onAuthenticationResult(). Sets the
     * AccountAuthenticatorResult which is sent back to the caller. Also sets
     * the authToken in AccountManager for this account.
     * 
     * @param the confirmCredentials result.
     */

     protected void  finishLogin() {
        Log.i(TAG, "finishLogin()");
        final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);

         if  (mRequestNewAccount) {
            mAccountManager.addAccountExplicitly(account, mPassword, null);
             // Set contacts sync for this account.
            ContentResolver.setSyncAutomatically(account,
                ContactsContract.AUTHORITY, true);
        }  else  {
            mAccountManager.setPassword(account, mPassword);
        }
        final Intent intent = new Intent();
         mAuthtoken = mPassword;
        intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
        intent
            .putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
         if  (mAuthtokenType != null
            && mAuthtokenType.equals(Constants.AUTHTOKEN_TYPE)) {
             intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthtoken);
        }
        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);
        finish();
    }
更多的内容请参考《 AbstractAccountAuthenticator简介 》和google的演示程序 SampleSyncAdapter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值