AbstractAccountAuthenticator简介

                本文翻译整理自:  http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html  

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>
 
The  android:resource  attribute must point to a resource that looks like:
Service应该有个名为 android.accounts.AccountAuthenticator 的 meta-data ,该 meta-data 的 resource 属性所指向的xml文件应该说明该Account在“ Accounts&sync ”中的基本显示信息
示例3:
 <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" /> 
示例4: authenticator文件(来自 SampleSyncAdapter )
 <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: icon, android: smallIcon, android: label 属性 表示的是你的Account在 Setting ->“ Accounts&sync ” 中进行显示时的图标和名字  。 android: accountType 表示的是你的Account的类型,它必须是唯一的。关于 Account 可参考 《 Account简介》 。 The  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:

示例5
<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 key KEY_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 call onResult(Bundle) or onError(int, String) when it is complete.

    当提供的参数不充足的时候,可以新建一个指向某个Activity的Intent,并把传进来的 AccountAuthenticatorResponse 参数以KEY_ACCOUNT_MANAGER_RESPONSE 为键放在Intent,然后把启动Activity的相关参数放入其中,最后把Intent以为键放在Bundle的,并返回该Bundle。

    这样系统将启动我们所指定的Activity,在Activity中完成工作后通过AccountAuthenticatorResponseonResult(Bundle) or onError(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 BundleaddAccount(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 BundleconfirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
Checks that the user knows the credentials of an account.
abstract BundleeditProperties(AccountAuthenticatorResponse response, String accountType)
Returns a Bundle that contains the Intent of the activity that can be used to edit the properties.
BundlegetAccountRemovalAllowed(AccountAuthenticatorResponse response, Account account)
Checks if the removal of this account is allowed.
abstract BundlegetAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
Gets the authtoken for an account.
abstract StringgetAuthTokenLabel(String authTokenType)
Ask the authenticator for a localized label for the given authTokenType.
final IBindergetIBinder()
abstract BundlehasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
Checks if the account supports all the specified authenticator specific features.
abstract BundleupdateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
Update the locally stored credentials for an account.
最后,关于 AbstractAccountAuthenticator 的使用请参照google官方的示例程 序SampleSyncAdapter 。
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值