注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。

原文链接:http://developer.android.com/training/sync-adapters/creating-authenticator.html


同步适配器框架假定你的同步适配器在同步数据时,设备存储会有一个账户,服务器存储端会有登录验证。因此,框架期望你提供一个叫做验证器的组件作为你的同步适配器的一部分。该组件会植入Android账户及认证框架,并提供一个标准的接口来处理用户凭据,比如登录信息。

甚至,如果你的应用不使用账户,你仍然需要提供一个认证器组件。如果你不使用账户或者服务器登录,认证器所处理的信息将被忽略,所以你可以提供一个认证器组件,它包括了一个空的实现。同时你需要提供一个捆绑的Service,来允许同步适配器框架来调用认证器的方法。

这节课将向你展示如何定义一个空验证器的所有满足其实现要求的部件。如果你想要提供一个真实的处理用户账户的验证器,可以阅读:AbstractAccountAuthenticator。


一). 添加一个空验证期组件

要在你的应用中添加一个空验证器,创建一个继承AbstractAccountAuthenticator的类,并将要覆写的方法置空(这样就不会做任何处理了),返回null或者抛出异常。

下面的代码片段是一个空验证器的例子:

/** Implement AbstractAccountAuthenticator and stub out all* of its methods*/
public class Authenticator extends AbstractAccountAuthenticator {// Simple constructorpublic Authenticator(Context context) {super(context);}// Editing properties is not supported
    @Overridepublic Bundle editProperties(AccountAuthenticatorResponse r, String s) {throw new UnsupportedOperationException();}// Don't add additional accounts
    @Overridepublic Bundle addAccount(AccountAuthenticatorResponse r,String s,String s2,String[] strings,Bundle bundle) throws NetworkErrorException {return null;}// Ignore attempts to confirm credentials
    @Overridepublic Bundle confirmCredentials(AccountAuthenticatorResponse r,Account account,Bundle bundle) throws NetworkErrorException {return null;}// Getting an authentication token is not supported
    @Overridepublic 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
    @Overridepublic String getAuthTokenLabel(String s) {throw new UnsupportedOperationException();}// Updating user credentials is not supported
    @Overridepublic Bundle updateCredentials(AccountAuthenticatorResponse r,Account account,String s, Bundle bundle) throws NetworkErrorException {throw new UnsupportedOperationException();}// Checking features for the account is not supported
    @Overridepublic Bundle hasFeatures(AccountAuthenticatorResponse r,Account account, String[] strings) throws NetworkErrorException {throw new UnsupportedOperationException();}
}


二). 将验证器绑定到框架

为了让同步适配器框架可以访问你的验证器,你必须为它创建一个捆绑服务。这一服务提供一个Android binder对象,允许框架调用你的验证器,并且在验证器和框架间传输数据。

因为框架会在它需要第一次访问验证器时启动Service,你也可以使用服务来实例化验证器,方法是通过在服务的Service.onCreate()方法中调用验证器的构造函数。

下面的代码样例展示了如何定义绑定Service:

/*** A bound Service that instantiates the authenticator* when started.*/
public class AuthenticatorService extends Service {...// Instance field that stores the authenticator objectprivate Authenticator mAuthenticator;@Overridepublic void onCreate() {// Create a new authenticator objectmAuthenticator = new Authenticator(this);}/** When the system binds to this Service to make the RPC call* return the authenticator's IBinder.*/@Overridepublic 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

将指明了用户账户类型的string本地化。如果你在“res/xml/syncadapter.xml”中通过指定“android:userVisible="true"”让同步适配器可见,那么你需要提供这个string。它会在系统的设置中的账户这一栏内显示,就在你定义的图标旁边。

下面的代码样例展示了你之前为验证器创建的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticatorxmlns: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"/>


四). 在清单文件中声明验证器

在之前的步骤中,你创建了一个捆绑服务,将验证器和同步适配器框架连接起来。要标识这个服务,你需要再清单文件中添加<service>标签,将它作为<application>的子标签:

    <serviceandroid:name="com.example.android.syncadapter.AuthenticatorService"><intent-filter><action android:name="android.accounts.AccountAuthenticator"/></intent-filter><meta-dataandroid:name="android.accounts.AccountAuthenticator"android:resource="@xml/authenticator" /></service>

标签<intent-filter>配置了一个由android.accounts.AccountAuthenticator的intent所激活的过滤器,这一intent会在系统要运行验证器时由系统发出。当过滤器被激活,系统会启动AuthenticatorService,它是你之前用来封装认证器的捆绑Service。

<meta-data>标签声明了验证器的元数据。android:name属性将元数据和验证器框架连接起来。android:resource指定了你之前所创建的认证器元数据文件的名字。

除了一个认证器,一个同步适配器框架需要一个内容提供器(content provider)。如果你的应用不适用内容提供器,可以阅读下一节课程,在下节课中将会创建一个空的内容提供器;如果你的应用适用的话,可以直接阅读:Creating a Sync Adapter。

转载于:https://www.cnblogs.com/jdneo/p/3654420.html

【Android Developers Training】 93. 创建一个空验证器相关推荐

  1. 【Android Developers Training】 20. 创建一个Fragment

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 104. 接受地点更新

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 【Android Developers Training】 0. 序言:构建你的第一个应用

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. 【Android Developers Training】 6. 配置Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 【Android Developers Training】 81. 解析XML数据

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. 【Android Developers Training】 58. 缓存位图

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 【Android Developers Training】 8. 定义Action Bar风格

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. 【Android Developers Training】 75. 使用NSD

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android Developers Training】 68. 序言:添加动画

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

最新文章

  1. 人脸识别引擎SeetaFaceEngine中Alignment模块使用的测试代码
  2. 如何查看和停止Linux启动的服务
  3. CentOS安装MongoDB
  4. 成功解决OpenVideoCall(不可用)以及MSB8020 The build tools for v141 (Platform Toolset = ‘v141‘) cannot be found
  5. python可选参数位置_每个位置参数的可选参数
  6. Fusioncharts图表组件在宿舍评分统计中的应用
  7. 手机MODEM 开发(28)--- VoLTE介绍
  8. 正反观点验证2010年10大安全挑战
  9. SensorManager
  10. 机器学习基石12-Nonlinear Transformation
  11. 艾伟:WM有约(一):你好,CF
  12. iwebAx产品家族之iweb SNS v0.6体验版--不错的开源软件~~
  13. 【前端 教程】详解 立即执行函数
  14. PLC可编程控制器的应用
  15. [音乐天堂]爱尔兰的小童星Declan
  16. 初探三维计算机视觉(三维重建) —— 相机模型 + 双目系统 + 点云模型
  17. 交互式电子白板有哪些功能
  18. 图论专题1(网络流)
  19. 网络划分之IP地址计算器
  20. Docker 使用OpenJDK 验证码无法显示

热门文章

  1. matlab计算运行时间方法
  2. 计算机内存知识txt,计算机新手必备内存实用知识.docx
  3. java---实现闹钟的基本功能
  4. POST与GET两种请求方式的区别:
  5. redis是单线程的吗?为什么执行速度这么快?
  6. 004_SpringBoot整合Listener
  7. mod php是什么意思,mod_php模式原理探析
  8. github pages markdown_排版利器——MarkDown入门简介
  9. 三角形周长最短问题_谈“最短”
  10. java创建医生的对象_基于安卓Android的作物医生App设计开发(MySQL)(含录像)