在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. [转]python中@classmethod @staticmethod区别

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...

  2. Java 8 Lambda排序 : Comparator example

    1. Classic Comparator example. Comparator<Developer> byName = new Comparator<Developer>( ...

  3. django -- 对行的更新只有在save调用后才会入库

    python3 manage.py shell Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) [GCC 4.2.1 (Apple In ...

  4. cocos2d-x 模态对话框的实现

    心情不好,恩.不扯淡了.直接讲. ================================== 在泰然看了一篇实现模态对话框的文章,写的还不错,然后在其基础上加了我简单加了一层灰色透明背景,这 ...

  5. Android Studio 通过 git update 或者 pull 的时候出错及解决办法

    Android Studio 通过 git update 或者 pull 的时候出错,log 如下: Couldn't save uncommitted changes. Tried to save ...

  6. STL学习笔记— —无序容器(Unordered Container)

    简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...

  7. error: unpacking of archive failed on file /usr/sbin/zabbix_agent;592e5bc3: cpio: open

    # lsattr /usr/ ----------I--e- /usr/lib64 ----------I--e- /usr/bin -------------e- /usr/libexec ---- ...

  8. 加快android studio 编译速度(已更新至Android Studio 3.3.1)

    1.加快AS启动速度 “Help”-"Edit Custom Properties...",在文件中输入 # custom Android Studio properties di ...

  9. unzip:unzip解压文件到指定目录

    1.把文件解压到当前目录下 unzip test.zip 2.如果要把文件解压到指定的目录下,需要用到-d参数. unzip -d /temp test.zip 3.解压的时候,有时候不想覆盖已经存在 ...

  10. lua -- io.pathinfo

    io.pathinfo 拆分一个路径字符串,返回组成路径的各个部分. 格式: parts = io.pathinfo(路径) 使用示例: local pathinfo = io.pathinfo(&q ...