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();
}