概述

在sdk目录下有个示例项目SampleSyncAdapter,演示了 用户授权和同步适配器的一些内容,是个学习的很好范例。我读了很久,很多地方没搞明白,先把理解的一些记录下来。

通过学习该示例,你可以做到:

  • 自定义的账户,让你的账户出现在 安卓系统设置的“账户”分组下。我看了下,像googel,华为,微信,支付宝都在这里出现。
  • 定义同步的适配器。这里我还没搞明白,似乎需要自定义contentProvider,到底什么时候会触发同步(同步频率控制),如何手动触发,等等,我还没搞懂

下面是我理解的代码里的一些class的作用:

authenticator包
    AuthenticationService  继承自Service,认证服务
  拿Authenticator作为成员,在onBind时返回mAuthenticator.getIBinder()

  Authenticator 继承AbstractAccountAuthenticator 验证器
  public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,String authTokenType,
                    String[] requiredFeatures, Bundle options)
   添加账户。返回一个intent,this intent 用于启动 登录页。

      public Bundle getAuthToken(AccountAuthenticatorResponse response,
               Account account,String authTokenType, Bundle loginOptions)
      获得 authToken。先从账户管理器里取密码,
如果能得到密码,则将账户密码使用NetworkUtilities.authenticate以获得authToken
如果未得到,则将用户名和账户类型发送到 登录页,以再次认证。

  AuthenticatorActivity ,继承自 AccountAuthenticatorActivity
登录页,显示用户名和密码输入框,等待用户输入。点击登录按钮后触发handleLogin事件,
在这里先验证用户输入非空,启动UserLoginTask。
在doInBackground中调用NetworkUtilities.authenticate获得authToken,
完成后保存用户名和密码。

client包

    NetworkUtilities:
public static String authenticate(String username, String password)
发送用户名和密码到服务端,收到 authToken字符串 public static List<RawContact> syncContacts(
Account account, String authtoken, long serverSyncState,
List<RawContact> dirtyContacts)
发送 用户名和authToken,serverSyncState, 和一个 联系人集合到服务端,
收到一个联系人集合 public static byte[] downloadAvatar(final String avatarUrl)
通过一个URL,获得用户头像的 字节集合
RawContact:
联系人信息的实体bean
notifier包
NotifierService: 通知服务,Service to handle view notifications.
This allows the sample sync adapter to update the information
when the contact is being looked at syncadapter syncadapter包
SyncAdapter:同步服务,继承自AbstractThreadedSyncAdapter
public void onPerformSync(Account account, Bundle extras,
String authority,ContentProviderClient provider, SyncResult syncResult)
处理同步,获得同步标记,获得需要同步的联系人,更新联系人,保存同步标记 SyncService:继承自Service
拿SyncAdapter作为成员,在onBind时返回 sSyncAdapter.getSyncAdapterBinder()
Constants 常量,声明 账户类型ACCOUNT_TYPE,授权标记类型AUTHTOKEN_TYPE

****************************************************************************************

AccountManager的常用方法

为指定账户设定密码
mAccountManager.setPassword(account, mPassword);

添加账户到 账户中心
final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);
mAccountManager.addAccountExplicitly(account, mPassword, null);

获得authToken
final String authtoken = mAccountManager.blockingGetAuthToken(account,
Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE);

获得用户数据
String markerString = mAccountManager.getUserData(account, SYNC_MARKER_KEY);

参考:

http://yarin.blog.51cto.com/1130898/479032/

http://www.cnblogs.com/fengzhblog/p/3177002.html
http://blog.csdn.net/wutianyin222/article/details/7911858
http://mobile.51cto.com/aprogram-392392.htm
http://mobile.51cto.com/aprogram-392388.htm

andrdoi示例项目SampleSyncAdapter分析的更多相关文章

  1. BookStore示例项目---菜单栏UI分析

    部署 参照 ABP示例项目BookStore搭建部署 项目解构 1).动态脚本代理 启动项目时,默认会调用两个接口 /Abp/ApplicationConfigurationScript /Abp/S ...

  2. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(4)

    这个系列已经写了6篇,链接地址如下: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Auto ...

  3. 转:Android官方MVP架构示例项目解析

    转自: http://www.infoq.com/cn/articles/android-official-mvp-architecture-sample-project-analysis 作者 吕英 ...

  4. Google官方MVP模式示例项目解析 todo-mvp

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6700668.html 引言:在Google没有给出一套权威的架构实现之前,很多App项目在架构方面都有或多 ...

  5. Aes加密/解密示例项目

    #AesEncrypt:Aes加密/解密示例项目 <br> 附件中的“AesEncrypt.zip”是本项目的exe文件,可直接下载下来运行和查看. *高级加密标准(英语:Advanced ...

  6. [Asp.net 5] DependencyInjection项目代码分析-目录

    微软DI文章系列如下所示: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Autofac [ ...

  7. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)

    Asp.net 5的依赖注入注入系列可以参考链接: [Asp.net 5] DependencyInjection项目代码分析-目录 我们在之前讲微软的实现时,对于OpenIEnumerableSer ...

  8. 我的ORM之示例项目

    我的ORM索引 示例项目 code.taobao.org/svn/MyMvcApp/ 1. 编译 MyTool ,DbEnt, WebApp, 安装JRE. 2. 配置 Web.config 数据库字 ...

  9. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(2)

    在 DependencyInjection项目代码分析4-微软的实现(1)中介绍了“ServiceTable”.“ServiceEntry”.“IGenericService”.“IService”. ...

随机推荐

  1. Android新特性--ConstraintLayout完全解析

    Android新特性--ConstraintLayout完全解析 本篇文章的主题是ConstraintLayout.其实ConstraintLayout是Android Studio 2.2中主要的新 ...

  2. Oozie workflow工作流action间参数传递实现

    假设workflow里有两个action节点,shell和hive,hive需要用到shell节点里的值,shell脚本如下 #!/bin/sh day=`date '+%Y%m%d%H'` echo ...

  3. struts2:图解action之HelloWorld示范(从action转到JSP)

    虽然Struts 2.x的Action在技术上不需要实现任何接口或继承任何类型,但是,大多情况下我们都会出于方便的原因,使Action类继承com.opensymphony.xwork2.Action ...

  4. HDU 1019 Least Common Multiple 数学题解

    求一组数据的最小公倍数. 先求公约数在求公倍数.利用公倍数,连续求全部数的公倍数就能够了. #include <stdio.h> int GCD(int a, int b) { retur ...

  5. ios支付宝问题整合

           1. 报错:rsa_private read error : private key is NULL     原因:私钥没有转成PKCS8   解决方法: 1)在RSADataSigne ...

  6. linux分区详解

    点评:如果你想了解linux分区,请务必认真阅读本文.对于初学者来说,linux分区不像windows下那么一目了解.    Linux 分区的规定      1. 设备管理 在 Linux 中,每一 ...

  7. nginx.conf中关于nginx-rtmp-module配置指令详解

    译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解.指令Corertmp语法:rtmp { ... }上下文:根描述:保存所 ...

  8. 【工具】Sublime + MarkdownEditing + OmniMarkupPreviewer使用起来

    Package Control的安装 下载安装Sublime Text3后,View -> Show Console调用Console. 在Console输入以下代码安装: import url ...

  9. 第二章 使用接口(Using Interfaces)-书籍翻译

    PDF预览 下载地址 http://files.cnblogs.com/DKSoft/CodingInDelphi.pdf 1.1. 解耦(Decoupling) All through this b ...

  10. VS2015打开项目慢、死机和卡顿解决方案

    VS2015功能强大,但是经常慢 网上有解决方案如下几个,留着备用: 1.选项-环境-基于客户端性能自动调整视觉体验,去掉选项. 2.选项-源代码管理-插件选无. 3.选项-文本编辑器-所有语言-滚动 ...