同步适配器框架假定您的同步适配器在与帐号关联的设备存储空间和需要登录访问的服务器存储空间之间传输数据。因此,框架希望您提供一个称为身份验证器的组件作为同步适配器的一部分。此组件可插入 Android 帐号和身份验证框架,并提供用于处理用户凭据(例如登录信息)的标准接口。
即使您的应用不使用帐号,您仍需要提供身份验证器组件。如果您不使用帐号或服务器登录,则身份验证器处理的信息将被忽略,因此您可以提供一个包含 stub 方法实现的身份验证器组件。您还需要提供一个 bound Service
,以便同步适配器框架调用身份验证器的方法。
本课程向您展示如何定义 stub 身份验证器的所有必需部分,以满足同步适配器框架的要求。如果您需要提供处理用户帐号的真实身份验证器,请阅读 AbstractAccountAuthenticator
的参考文档。
添加 stub 身份验证器组件
要将 stub 身份验证器组件添加到您的应用,请创建一个扩展 AbstractAccountAuthenticator
的类,然后通过返回 null
或抛出异常来 stub 出所需的方法。
以下代码段显示了一个 stub 身份验证器类的示例
Kotlin
/* * Implement AbstractAccountAuthenticator and stub out all * of its methods */ class Authenticator(context: Context) // Simple constructor : AbstractAccountAuthenticator(context) { // Editing properties is not supported override fun editProperties(r: AccountAuthenticatorResponse, s: String): Bundle { throw UnsupportedOperationException() } // Don't add additional accounts @Throws(NetworkErrorException::class) override fun addAccount( r: AccountAuthenticatorResponse, s: String, s2: String, strings: Array<String>, bundle: Bundle ): Bundle? = null // Ignore attempts to confirm credentials @Throws(NetworkErrorException::class) override fun confirmCredentials( r: AccountAuthenticatorResponse, account: Account, bundle: Bundle ): Bundle? = null // Getting an authentication token is not supported @Throws(NetworkErrorException::class) override fun getAuthToken( r: AccountAuthenticatorResponse, account: Account, s: String, bundle: Bundle ): Bundle { throw UnsupportedOperationException() } // Getting a label for the auth token is not supported override fun getAuthTokenLabel(s: String): String { throw UnsupportedOperationException() } // Updating user credentials is not supported @Throws(NetworkErrorException::class) override fun updateCredentials( r: AccountAuthenticatorResponse, account: Account, s: String, bundle: Bundle ): Bundle { throw UnsupportedOperationException() } // Checking features for the account is not supported @Throws(NetworkErrorException::class) override fun hasFeatures( r: AccountAuthenticatorResponse, account: Account, strings: Array<String> ): Bundle { throw UnsupportedOperationException() } }
Java
/* * Implement AbstractAccountAuthenticator and stub out all * of its methods */ public class Authenticator extends AbstractAccountAuthenticator { // Simple constructor public Authenticator(Context context) { super(context); } // Editing properties is not supported @Override public Bundle editProperties( AccountAuthenticatorResponse r, String s) { throw new UnsupportedOperationException(); } // Don't add additional accounts @Override public Bundle addAccount( AccountAuthenticatorResponse r, String s, String s2, String[] strings, Bundle bundle) throws NetworkErrorException { return null; } // Ignore attempts to confirm credentials @Override public Bundle confirmCredentials( AccountAuthenticatorResponse r, Account account, Bundle bundle) throws NetworkErrorException { return null; } // Getting an authentication token is not supported @Override public Bundle getAuthToken( AccountAuthenticatorResponse r, Account account, String s, Bundle bundle) throws NetworkErrorException { throw new UnsupportedOperationException(); } // Getting a label for the auth token is not supported @Override public String getAuthTokenLabel(String s) { throw new UnsupportedOperationException(); } // Updating user credentials is not supported @Override public Bundle updateCredentials( AccountAuthenticatorResponse r, Account account, String s, Bundle bundle) throws NetworkErrorException { throw new UnsupportedOperationException(); } // Checking features for the account is not supported @Override public Bundle hasFeatures( AccountAuthenticatorResponse r, Account account, String[] strings) throws NetworkErrorException { throw new UnsupportedOperationException(); } }
将身份验证器绑定到框架
为了让同步适配器框架访问您的身份验证器,您必须为其创建一个 bound Service。此服务提供了一个 Android binder 对象,允许框架调用您的身份验证器并在身份验证器和框架之间传递数据。
以下代码段向您展示如何定义 bound Service
Kotlin
/** * A bound Service that instantiates the authenticator * when started. */ class AuthenticatorService : Service() { // Instance field that stores the authenticator object private lateinit var mAuthenticator: Authenticator override fun onCreate() { // Create a new authenticator object mAuthenticator = Authenticator(getApplicationContext()) } /* * When the system binds to this Service to make the RPC call * return the authenticator's IBinder. */ override fun onBind(intent: Intent?): IBinder = mAuthenticator.iBinder }
Java
/** * A bound Service that instantiates the authenticator * when started. */ public class AuthenticatorService extends Service { ... // Instance field that stores the authenticator object private Authenticator mAuthenticator; @Override public void onCreate() { // Create a new authenticator object mAuthenticator = new Authenticator(getApplicationContext()); } /* * When the system binds to this Service to make the RPC call * return the authenticator's IBinder. */ @Override public IBinder onBind(Intent intent) { return mAuthenticator.getIBinder(); } }
添加身份验证器元数据文件
要将您的身份验证器组件接入同步适配器和帐号框架,您需要为这些框架提供描述该组件的元数据。此元数据声明了您为同步适配器创建的帐号类型,并声明了如果您希望帐号类型对用户可见时系统显示的界面元素。在您的应用项目中的 /res/xml/
目录中存储的 XML 文件中声明此元数据。您可以为该文件指定任意名称,不过通常称为 authenticator.xml
。
此 XML 文件包含一个单独的元素 <account-authenticator>
,具有以下属性
-
android:accountType
- 同步适配器框架要求每个同步适配器都必须具有帐号类型,采用域名形式。框架使用帐号类型作为同步适配器内部标识的一部分。对于需要登录的服务器,帐号类型与用户帐号一起发送到服务器,作为登录凭据的一部分。
如果您的服务器不需要登录,您仍然需要提供帐号类型。对于该值,请使用您控制的域名。尽管框架使用它来管理您的同步适配器,但该值不会发送到您的服务器。
-
android:icon
- 指向包含图标的 Drawable 资源。如果您通过在
res/xml/syncadapter.xml
中指定属性android:userVisible="true"
来使同步适配器可见,则必须提供此图标资源。它会出现在系统“设置”应用的帐号部分中。 -
android:smallIcon
- 指向包含图标小版本的 Drawable 资源。根据屏幕大小,此资源可能会在系统“设置”应用的帐号部分中使用,而非使用
android:icon
。 -
android:label
- 用于向用户标识帐号类型的本地化字符串。如果您通过在
res/xml/syncadapter.xml
中指定属性android:userVisible="true"
来使同步适配器可见,则应提供此字符串。它会出现在系统“设置”应用的帐号部分中,紧邻您为身份验证器定义的图标。
以下代码段显示了您之前创建的身份验证器的 XML 文件
<?xml version="1.0" encoding="utf-8"?> <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="example.com" android:icon="@drawable/ic_launcher" android:smallIcon="@drawable/ic_launcher" android:label="@string/app_name"/>
在清单中声明身份验证器
在之前的步骤中,您创建了一个将身份验证器链接到同步适配器框架的 bound Service
。要向系统标识此服务,请在您的应用清单中声明它,方法是添加以下 <service>
元素作为 <application>
的子元素
<service android:name="com.example.android.syncadapter.AuthenticatorService"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator"/> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" /> </service>
<intent-filter>
元素设置了一个过滤器,该过滤器由意图操作 android.accounts.AccountAuthenticator
触发,该操作由系统发送以运行身份验证器。当过滤器触发时,系统会启动 AuthenticatorService
,即您为封装身份验证器而提供的 bound Service
。
<meta-data>
元素声明身份验证器的元数据。android:name
属性将元数据链接到身份验证框架。android:resource
元素指定您之前创建的身份验证器元数据文件的名称。
除了身份验证器,同步适配器还需要一个内容提供程序。如果您的应用尚未用到内容提供程序,请前往下一课了解如何创建 stub 内容提供程序;否则,请前往课程 创建同步适配器。