在android的系统设置页,有个“账户”分组,里面有很多的账户,很多app都使用了这个账户系统,比如“谷歌”,“淘宝”,“微信”,“华为”等。这些都是大公司呢,有没有可能我们也使用这个,让我们的软件的名称也出现在这里呢,答应是肯定的。看看效果图:

   

加上这个,我们的app立马就高大上了,如何做到呢?

实现步骤

1. 继承 AbstractAccountAuthenticator 实现 一个 自己的 账户认证器

2. 继承自 service,实现一个服务,该服务使用上一步的 账户认证器

3. 继承 AccountAuthenticatorActivity ,实现 自定义的 登录页面。

知识准备

你需要了解 上面积个类的知识,和accountManager,我翻译了这几个类的概述,参考我前几篇文章。

分析

如果被系统发现?

自定义的服务代码如下:

    <service
android:name=".MyAuthenticatorService"
android:exported="true" >
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter> <meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>

我们看到,这个service 指定了过滤器为 android.accounts.AccountAuthenticator,这是个系统调用的 action。

自定义 认证器 做了什么?

我们需要在定义认证器里实现 它的 addAccount 和 getAuthToken 方法。

addAccount方法: 在api里解释说,在 添加账户时,需要从用户收集认证信息(账户名和密码),需要返回一个intent,以打开一个activity,这个activity就是我们登陆页了。我们需要在登录页里处理登录的逻辑实现,并使用AccountManager记录下 登录成功的用户信息,并保存在账户中心。

getAuthToken方法: 获得auth Token ,这是一个授权标记,有些地方翻译为“令牌”,就是从服务端返回的字符串,指示了登录成功。很多系统不在本地(手机)保存真实的密码,配合auth token 来显示一次登录认证,后续的操作请求都携带auth token作为身份令牌。

    @Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType, String authTokenType,
String[] requiredFeatures, Bundle options)
throws NetworkErrorException {
// 需要实现
Log.v(TAG, "addAccount()");
final Intent intent = new Intent(mContext, LoginActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
} @Override
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account, String authTokenType, Bundle options)
throws NetworkErrorException {
// 需要实现
return null;
}

具体实现代码

可以到我的云盘下载:  http://yunpan.cn/cAIvcxDHZjrIS 访问密码 d8f7

android开发(39) 使用android系统的账户中心管理账的更多相关文章

  1. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  2. Android开发_如何调用系统默认浏览器访问

    Android开发_如何调用系统默认浏览器访问 2015-10-20 17:53 312人阅读  http://blog.sina.com.cn/s/blog_6efce07e010142w7.htm ...

  3. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen深入理解

    原文链接:Hello, Android Multiscreen_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android Multiscreen深入理解. 本 ...

  4. [译]:Xamarin.Android开发入门——Hello,Android深入理解

    返回索引目录 原文链接:Hello, Android_DeepDive. 译文链接:Xamarin.Android开发入门--Hello,Android深入理解 本部分介绍利用Xamarin开发And ...

  5. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  6. Android开发周报:Android L默认加密用户数据

    Android开发周报:Android L默认加密用户数据 新闻 <iCloud前车之鉴,Android L默认开启加密功能>:iCloud 艳照风波再起,第二波女星照片流出,大量女星的裸 ...

  7. Android开发工程师文集-Android知识点讲解

    前言 大家好,给大家带来Android开发工程师文集-Android知识点讲解的概述,希望你们喜欢 WebView讲解 一般通过Intent调用系统的浏览器: Uri uri = Uri.parse( ...

  8. Centos Android开发环境配置-Android Tools -android list sdk --extended --all

    Centos Android开发环境配置-Android Tools -android  list sdk --extended --all 安装完Android Tools后执行 android   ...

  9. 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。

    AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...

随机推荐

  1. 容错处理库Polly使用文档

    Design For Failure1. 一个依赖服务的故障不会严重破坏用户的体验.2. 系统能自动或半自动处理故障,具备自我恢复能力. 以下是一些经验的服务容错模式 超时与重试(Timeout an ...

  2. SpringBoot+mybatis实现多数据源支持

    什么是多数据源支持? 简单的说,就是一个项目里,同时可以访问多个不同的数据库. 实现原理 单个数据源在配置时会绑定一套mybatis配置,多个数据源时,不同的数据源绑定不同的mybatis配置就可以了 ...

  3. 【android开发】如何在Linux平台下安装JDK环境

    原文:http://android.eoe.cn/topic/android_sdk Linux平台JDK安装 本文主要描述如何在Linux平台下安装JDK环境.进入网页:http://www.ora ...

  4. Android 4.1的新特性介绍

    原文:http://android.eoe.cn/topic/summary 果冻豆 - Android 4.1 通知系统 - Notifications 在Android 4.1系统上通知的功能大大 ...

  5. sql字段组合唯一

    create unique index [Itenmid_Uid] on Userchangeinfo(Itemid,Uid)

  6. FFmpeg编译: undefined reference to 'av_frame_alloc()'

    今天使用CMake编译FFmpeg的时候,死活编不过,提示什么“undefined reference to 'av_frame_alloc()” 后来仔细查找,发现是头文件包含错误. 错误的代码: ...

  7. Eclipse 配置Maven以及修改默认Repository

    今天将Eclipse关于Maven的配置总结一下,方便以后配置. 一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本,地址:http://mirrors.cnnic.cn ...

  8. linux命令(46):程序运行前后台切换

    A,Shell支持作用控制,有以下命令:1. command& 让进程在后台运行2. jobs 查看后台运行的进程3. fg %n 让后台运行的进程n到前台来4. bg %n 让进程n到后台去 ...

  9. python 三元表达式 if for 构建List 进阶用法

    1.简单的for...[if]...语句 Python中,for...[if]...语句一种简洁的构建List的方法,从for给定的List中选择出满足if条件的元素组成新的List,其中if是可以省 ...

  10. Node和Electron开发入门(四):操作PC端文件系统

    一.调用PC端默认方式打开本地文件 在main.js里 // 打开系统本地文件或者网页链接 const {shell} = require('electron'); // Open a local f ...