【Android Developers Training】 93. 创建一个空验证器
注:本文翻译自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 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();
}
}
二). 将验证器绑定到框架
为了让同步适配器框架可以访问你的验证器,你必须为它创建一个捆绑服务。这一服务提供一个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 object
private Authenticator mAuthenticator;
@Override
public void onCreate() {
// Create a new authenticator object
mAuthenticator = new Authenticator(this);
}
/*
* 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
将指明了用户账户类型的string本地化。如果你在“res/xml/syncadapter.xml”中通过指定“android:userVisible="true"”让同步适配器可见,那么你需要提供这个string。它会在系统的设置中的账户这一栏内显示,就在你定义的图标旁边。
下面的代码样例展示了你之前为验证器创建的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"/>
四). 在清单文件中声明验证器
在之前的步骤中,你创建了一个捆绑服务,将验证器和同步适配器框架连接起来。要标识这个服务,你需要再清单文件中添加<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的intent所激活的过滤器,这一intent会在系统要运行验证器时由系统发出。当过滤器被激活,系统会启动AuthenticatorService,它是你之前用来封装认证器的捆绑Service。
<meta-data>标签声明了验证器的元数据。android:name属性将元数据和验证器框架连接起来。android:resource指定了你之前所创建的认证器元数据文件的名字。
除了一个认证器,一个同步适配器框架需要一个内容提供器(content provider)。如果你的应用不适用内容提供器,可以阅读下一节课程,在下节课中将会创建一个空的内容提供器;如果你的应用适用的话,可以直接阅读:Creating a Sync Adapter。
【Android Developers Training】 93. 创建一个空验证器的更多相关文章
- 【Android Developers Training】 92. 序言:使用同步适配器传输数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 94. 创建一个空内容提供器(Content Provider)
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 95. 创建一个同步适配器
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 21. 创建一个可变动的UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 1. 创建一个Android项目工程
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 18. 重新创建一个Activity
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 20. 创建一个Fragment
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 3. 构建一个简单UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 106. 创建并检测地理围栏
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
随机推荐
- [刷题]算法竞赛入门经典(第2版) 4-3/UVa220 - Othello
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa 220 - Othello #include<iostream ...
- JDK源码之AQS源码剖析
除特别注明外,本站所有文章均为原创,转载请注明地址 AbstractQueuedSynchronizer(AQS)是JDK中实现并发编程的核心,平时我们工作中经常用到的ReentrantLock,Co ...
- vue1.0和vue2.0的区别(二)
这篇我们继续之前的vue1.0和vue2.0的区别(一)继续说 四.循环 学过vue的同学应该知道vue1.0是不能添加重复数据的,否则它会报错,想让它重复添加也不是不可以,不过需要定义别的东西 而v ...
- 用kotlin方式打开《第一行代码:Android》
参考:<第一行代码:Android>第2版--郭霖 注1:本文为原创,例子可参考郭前辈著作:<第一行代码:Android> 注2:本文不赘述android开发的基本理论,不介绍 ...
- Javascript开发技巧(JS中的变量、运算符、分支结构、循环结构)
一.Js简介和入门 继续跟进JS开发的相关教程. <!-- [使用JS的三种方式] 1.HTML标签中内嵌JS(不提倡使用): 示例:<button onclick="javas ...
- 为什么重写equals时必须重写hashCode方法?(转发+整理)
为什么重写equals时必须重写hashCode方法? 原文地址:http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html ...
- 013 session_flush
在hibernate中也存在flush这个功能,在默认的情况下session.commit()之前时,其实执行了一个flush命令. Session.flush功能: ②理缓存: ②执行sql(确定是 ...
- 四、 添加模型Model(ASP.NET MVC5 系列)
在这一章节中我们将添加一些classes类来管理数据库中的movies.这些classes类就是ASP.NET MVC应用程序中的"model". 我们将用.NET框架中的数据访问 ...
- 全景智慧城市——VR全景,开启VR营销新时代
全景是一种新兴的富媒体技术. 与视频.声音.图片等传统主流媒体最大的区别是"可操作,可交互". 全景给人以三维立体感觉的实景360°全方位图像,此图像最大的三个特点: 全方位:展示 ...
- 侯捷STL学习(二)
第六节:容器之分类和各种测试(四) stack不提供iterator操作,破坏了容器的独特性,先进先出. 使用容器multiset(允许元素重复) 内部是红黑树,insert操作就保证了排好了序. 标 ...