前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发(38) 账户中心和同步适配器

Android开发(38) 账户中心和同步适配器

作者头像
张云飞Vir
发布2020-03-16 15:23:11
7160
发布2020-03-16 15:23:11
举报
文章被收录于专栏:写代码和思考写代码和思考

概述

在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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 我的理解
    • authenticator包
      • client包
        • notifier包
          • syncadapter包
            • AccountManager的常用方法
            • 为指定账户设定密码
            • 添加账户到 账户中心
              • 获得authToken
                • 获得用户数据
                • 参考:
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档