在android提供的sdk中,samples目录下有一个叫SampleSyncAdapter的示例,它是一个账号与同步的实例,比如Google原始的android手机可以使用Google账号进行数据的同步。具体的比如你想实时同步你的通讯录到服务端,这时候你就可以通过这个实例来了解android提供的同步机制,从而实现自己的同步功能。先介绍一下账号的管理部分。至于账号管理的代码主要是在authenticator包下的三个类里面,还有就是一个叫authenticator 的xml文件。

AuthenticationService类

AuthenticationService是一个继承Service的服务,这个服务其实是提供给其他的进程使用的,android系统会通过"android.accounts.AccountAuthenticator"这个Action找到它,并通过它来把我们自己的账号注册到“设置”中,其实这是一个AIDL的使用,它属于跨进程的调用。下面是manifest中的注册:

  <serviceandroid:name=".authenticator.AuthenticationService"android:exported="true"><intent-filter><actionandroid:name="android.accounts.AccountAuthenticator" /></intent-filter><meta-dataandroid:name="android.accounts.AccountAuthenticator"android:resource="@xml/authenticator" /></service>

这个Service会在onBind方法里返回一个IBinder给客户端进程,如下:

    @Overridepublic IBinder onBind(Intent intent) {if (Log.isLoggable(TAG, Log.VERBOSE)) {Log.v(TAG, "getBinder()...  returning the AccountAuthenticator binder for intent "+ intent);}return mAuthenticator.getIBinder();}

Authenticator类

Authenticator是一个继承自AbstractAccountAuthenticator的类,AbstractAccountAuthenticator是一个虚类,它定义处理手机“设置”里“账号与同步”中Account的添加、删除和验证等功能的基本接口,并实现了一些基本功能。AbstractAccountAuthenticator里面有个继承于IAccountAuthenticator.Stub的内部类,以用来对AbstractAccountAuthenticator的远程接口调用进行包装。我们可以通过AbstractAccountAuthenticator的getIBinder()方法,返回内部类的IBinder形式,以便对此类进行远程调用,如上面代码onBind方法中的调用。AbstractAccountAuthenticator的源码位置在frameworks\base\core\java\android\accounts目录下。Authenticator只需要继承和实现AbstractAccountAuthenticator的几个方法就可以了,像我们所介绍的SampleSyncAdapter实例主要继承了两个方法,如下

//当在“设置”中添加账号时,会调用这个方法,跳转到添加账号页面
@Overridepublic Bundle addAccount(AccountAuthenticatorResponse response, String accountType,String authTokenType, String[] requiredFeatures, Bundle options) {Log.v(TAG, "addAccount()");//指定AuthenticatorActivity为添加账号的页面,下面会介绍。final Intent intent = new Intent(mContext, AuthenticatorActivity.class);intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);final Bundle bundle = new Bundle();bundle.putParcelable(AccountManager.KEY_INTENT, intent);return bundle;}//当执行mAccountManager.blockingGetAuthToken(account,Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE);时调用该方法。@Overridepublic Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,String authTokenType, Bundle loginOptions) throws NetworkErrorException {Log.v(TAG, "getAuthToken()");
// 通过blockingGetAuthToken方法传来的Constants.AUTHTOKEN_TYPEif (!authTokenType.equals(Constants.AUTHTOKEN_TYPE)) {final Bundle result = new Bundle();result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");return result;}
final AccountManager am = AccountManager.get(mContext);final String password = am.getPassword(account);if (password != null) {final String authToken = NetworkUtilities.authenticate(account.name, password);if (!TextUtils.isEmpty(authToken)) {//如果已经到服务器验证过账号并保存到AccountManager中final Bundle result = new Bundle();result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);result.putString(AccountManager.KEY_AUTHTOKEN, authToken);return result;}}
//如果没有到服务器验证过账号并保存到AccountManager中,则重新倒添加账号页面中验证。final Intent intent = new Intent(mContext, AuthenticatorActivity.class);intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);final Bundle bundle = new Bundle();bundle.putParcelable(AccountManager.KEY_INTENT, intent);return bundle;}

AuthenticatorActivity类

AuthenticatorActivity是一个继承自AccountAuthenticatorActivity的 activity,AccountAuthenticatorActivity的源码也是在frameworks\base\core\java \android\accounts目录下。AuthenticatorActivity主要的一个方法是handleLogin(View view),当点击Sign in按钮时会调用该方法,该方法会启动一个异步任务来请求服务器验证用户账号。验证成功后有

一个重要的方法:

/*** Called when response is received from the server for authentication* request. See onAuthenticationResult(). Sets the* AccountAuthenticatorResult which is sent back to the caller. We store the* authToken that's returned from the server as the 'password' for this* account - so we're never storing the user's actual password locally.** @param result the confirmCredentials result.*/private void finishLogin(String authToken) {
Log.i(TAG, "finishLogin()");final Account account = new Account(mUsername, Constants.ACCOUNT_TYPE);if (mRequestNewAccount) {//直接向AccountManager添加一个帐户mAccountManager.addAccountExplicitly(account, mPassword, null);//设置让这个账号能够自动同步ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);} else {mAccountManager.setPassword(account, mPassword);}final Intent intent = new Intent();intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);setAccountAuthenticatorResult(intent.getExtras());setResult(RESULT_OK, intent);finish();}

authenticator.xml

在上面的AuthenticationService注册中有个meta-data的名字为android.accounts.AccountAuthenticator,它所指向的xml文件是authenticator.xml,其内容如下:

<account-authenticator xmlns:android=" http://schemas.android.com/apk/res/android "android:accountType="com.example.android.samplesync"android:icon="@drawable/icon"android:smallIcon="@drawable/icon"android:label="@string/label"
/>

其中账户类型为com.example.android.samplesync,就是Constants.ACCOUNT_TYPE的值。这个有点像widget,需要一个xml提供你想要的信息。

Android账号管理机制相关推荐

  1. android 账号同步功能吗,android账号管理与同步机制

    android账号管理在手机设置的其他账户中可以找到,打开后,发现这里有许多有趣的功能,比如微信的同步通讯录.天翼云账户的一键跳转修改账户密码等,接下来,我们来讲讲他的实现思路 实验原理 效果图 几个 ...

  2. Android内存管理机制

    好文摘录 原作: https://www.cnblogs.com/nathan909/p/5372981.html 1.基于Linux内存管理 Android系统是基于Linux 2.6内核开发的开源 ...

  3. Android 系统(203)---Android包管理机制(一)PackageInstaller的初始化

    Android包管理机制(一)PackageInstaller的初始化 转自:https://blog.csdn.net/itachi85/article/details/81024903 前言 包管 ...

  4. Android包管理机制(三)PMS处理APK的安装

    本文首发于微信公众号「刘望舒」 关联系列 Android包管理机制系列 前言 在上一篇文章Android包管理机制(二)PackageInstaller安装APK中,我们学习了PackageInsta ...

  5. Android包管理机制5 APK是如何被解析的

    一 概述 在本系列的前面文章中,我们介绍了 PackageInstaller 的初始化和安装 APK 过程.PMS 处理 APK 的安装和 PMS 的创建过程,这些文章中经常会涉及到一个类,那就是 P ...

  6. com.android.packageinstaller,Android包管理机制(二)PackageInstaller安装APK

    前言 在本系列上一篇文章Android包管理机制(一)PackageInstaller的初始化中我们学习了PackageInstaller是如何初始化的,这一篇文章我们接着学习PackageInsta ...

  7. Android内存管理机制官方详解文档

    很早之前写过一篇<Android内存管理机制详解>点击量已7万+,现把Google官方文档整理输出一下,供各位参考. 一.内存管理概览 Android 运行时 (ART) 和 Dalvik ...

  8. Android内存管理机制详解,【安卓知识普及贴】之关于安卓内存管理机制的一些介绍和阐述。...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 安卓系统其实不需要极度精简,只是一味的精简并不会给系统带来什么好处反而只有害处,因为许多系统应用跟设置里的选项有关联,删除掉就会在选择关联选项的时候FC, ...

  9. Android 内存管理机制

    本文主要包括三大部分内容: 内存管理基础:从整个计算机领域简述主要的内存管理技术. Linux的内存管理机制:Android毕竟是基于Linux内核实现的操作系统,因此有必要了解一下Linux的内存管 ...

最新文章

  1. 别了比特币,BCH将领导加密货币!
  2. mysql phpwind_【phpwind教程:Linux Apache php MySQL GD PHPWind 集成环境配置】-phpwind教程-【零度中国】...
  3. SpringBoot Web原生组件注入(Servlet、Filter、Listener)
  4. 【Python】Python中的for循环,没你想的那么简单~
  5. [PE格式分析] 3.IMAGE_NT_HEADER
  6. Linux系统编程--1(进程和程序,CPU和MMU,PCB,进程状态)
  7. STM32F0使用LL库实现PWM输出
  8. mui请求php,PHP 怎样处理mui.ajax POST过来的数据?
  9. IT行业学习网站汇总
  10. CentOS操作记录
  11. 题解 CF1399D 【Binary String To Subsequences】
  12. 彩色静电植绒印花工艺的五个方法
  13. CAD入门教程,基本设置,使用技巧
  14. 全局快门和卷帘快门的区别
  15. ios 图片居中裁剪_iOS 自定义裁剪相册照片
  16. html5光线传感器,HTML5设备传感器总结
  17. 华为路由器交换机常用命令(随时补充更新)
  18. manifest权限
  19. 保险入门,我不推荐买保险
  20. HC05蓝牙模块配对步骤及AT模式设置方法

热门文章

  1. Documentum常见问题4—如何通过vlink方式直接查看文档内容【转】
  2. 完美国际服务器管理修改经验倍数,魔兽地图编辑问题--修改经验倍数
  3. 安装SSL证书的网站被谷歌提升排名权重
  4. windows7现实计算机内存不足,win7 64位有剩余空间却提示内存不足的解决方法
  5. 命令执行原理和利用知识点
  6. 跟它比,期货简直 Low 爆了!
  7. 2021年全国职业院校技能大赛:网络系统管理项目-真题-模块C-2全套视频讲解含无线地勘
  8. 分形造型的常用模型,C构造简单的IFS图形
  9. JS中的`DOM`增删修
  10. 海思Hi3559AV100移植Qt5.9.9(一)