Abstract base class for creating AccountAuthenticators. In order to be an authenticator one must extend this class, provider implementations for the abstract methods and write a service that returns the result of getIBinder()
in the service's onBind(android.content.Intent)
when invoked with an intent with actionACTION_AUTHENTICATOR_INTENT
. This service must specify the following intent filter and metadata tags in its AndroidManifest.xml file
android.accounts.AbstractAccountAuthenticator是一个虚类,它定义处理Setting->“Accounts&sync”中Account的添加和验证等功能的基本接口,并实现了一些基本功能。AbstractAccountAuthenticator里面有个继承于IAccountAuthenticator.Stub的内部类,以用来对AbstractAccountAuthenticator的远程接口调用进行包装。我们可以通过AbstractAccountAuthenticator的getIBinder()方法,返回内部类的IBinder形式,以便对AbstractAccountAuthenticator进行远程调用。
为了能让系统找到我们自制的Account的添加和验证等功能的接口函数,我们需要如下定义一个能接受action为ACTION_AUTHENTICATOR_INTENT
的Intent的Service,并在Service的onBind(android.content.Intent)中调AbstractAccountAuthenticator的getIBinder()
返回其用于远程调用的IBinder。
示例1:
<intent-filter> <action android:name="android.accounts.AccountAuthenticator" /> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" />
示例2(来自SampleSyncAdapter):
<!-- The authenticator service -->
<service android:name=".authenticator.AuthenticationService" android:exported="true"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator" /> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" /> </service>
android:resource
attribute must point to a resource that looks like:
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="typeOfAuthenticator" android:icon="@drawable/icon" android:smallIcon="@drawable/miniIcon" android:label="@string/label" android:accountPreferences="@xml/account_preferences" />
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.example.android.samplesync"
android:icon="@drawable/icon"
android:smallIcon="@drawable/icon"
android:label="@string/label"
/>
android:accountType
attribute must be a string that uniquely identifies your authenticator and will be the same string that user will use when making calls on the
AccountManager
and it also corresponds to
type
for your accounts. 使用的android:icon属性的一个地方在
Setting
->“
Accounts&sync
”
设置 ,使用android:smallIcon 属性的一个地方是在Contact Application's tab panels.
android:accountPreferences 属性应该指向一个referenceScreen xml hierarchy that contains a list of PreferenceScreens that can be invoked to manage the authenticator. An example is:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/title_fmt" /> <PreferenceScreen android:key="key1" android:title="@string/key1_action" android:summary="@string/key1_summary"> <intent android:action="key1.ACTION" android:targetPackage="key1.package" android:targetClass="key1.class" /> </PreferenceScreen> </PreferenceScreen>
The standard pattern for implementing any of the abstract methods is the following:
- If the supplied arguments are enough for the authenticator to fully satisfy the request then it will do so and return a
Bundle
that contains the results.当提供的参数已经足够的时候,可以直接用Bundler返回结果。 - If the authenticator needs information from the user to satisfy the request then it will create an
Intent
to an activity that will prompt the user for the information and then carry out the request. This intent must be returned in a Bundle as keyKEY_INTENT
.The activity needs to return the final result when it is complete so the Intent should contain the
AccountAuthenticatorResponse
asKEY_ACCOUNT_MANAGER_RESPONSE
. The activity must then callonResult(Bundle)
oronError(int, String)
when it is complete.当提供的参数不充足的时候,可以新建一个指向某个Activity的Intent,并把传进来的 AccountAuthenticatorResponse 参数以KEY_ACCOUNT_MANAGER_RESPONSE 为键放在Intent,然后把启动Activity的相关参数放入其中,最后把Intent以为键放在Bundle的,并返回该Bundle。
这样系统将启动我们所指定的Activity,在Activity中完成工作后通过AccountAuthenticatorResponse的
onResult(Bundle)
oronError(int, String)
返回结果 - If the authenticator cannot synchronously process the request and return a result then it may choose to return null and then use the AccountManagerResponse to send the result when it has completed the request.如果我们想进行的操作是异步操作,而不是同步,可以直接返回null,在操作真正完成后才通过 AccountAuthenticatorResponse 返回操作结果。
The following descriptions of each of the abstract authenticator methods will not describe the possible asynchronous nature of the request handling and will instead just describe the input parameters and the expected result.
When writing an activity to satisfy these requests one must pass in the AccountManagerResponse and return the result via that response when the activity finishes (or whenever else the activity author deems it is the correct time to respond). The AccountAuthenticatorActivity
handles this, so one may wish to extend that when writing activities to handle these requests.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract Bundle |
addAccount(
AccountAuthenticatorResponse response,
String accountType,
String authTokenType,
String[] requiredFeatures,
Bundle options)
Adds an account of the specified accountType.
添加一个我们声明的Account时调用。
注意:
accountType是在Mainfest.xml中通过Service的meta-data的
android.accounts.AccountAuthenticator属性指向的xml文件的android:accountType属性来声明的
| ||||||||||
abstract Bundle |
confirmCredentials(
AccountAuthenticatorResponse response,
Account account,
Bundle options)
Checks that the user knows the credentials of an account.
| ||||||||||
abstract Bundle |
editProperties(
AccountAuthenticatorResponse response,
String accountType)
Returns a Bundle that contains the Intent of the activity that can be used to edit the properties.
| ||||||||||
Bundle |
getAccountRemovalAllowed(
AccountAuthenticatorResponse response,
Account account)
Checks if the removal of this account is allowed.
| ||||||||||
abstract Bundle |
getAuthToken(
AccountAuthenticatorResponse response,
Account account,
String authTokenType,
Bundle options)
Gets the authtoken for an account.
| ||||||||||
abstract String |
getAuthTokenLabel(
String authTokenType)
Ask the authenticator for a localized label for the given authTokenType.
| ||||||||||
final IBinder | getIBinder() | ||||||||||
abstract Bundle |
hasFeatures(
AccountAuthenticatorResponse response,
Account account,
String[] features)
Checks if the account supports all the specified authenticator specific features.
| ||||||||||
abstract Bundle |
updateCredentials(
AccountAuthenticatorResponse response,
Account account,
String authTokenType,
Bundle options)
Update the locally stored credentials for an account.
|