android.accounts主要包括了集中式的帐户管理API,

AccountManagerCallback,

AccountManagerFuture,

OnAccountsUpdateListener,

AbstractAccountAuthenticator,

Account,

AccountAuthenticatorActivity,

AccountAuthenticatorResponse,

AccountManager,

AuthenticatorDescription,

示例学习:添加多个帐户来集中管理

1. 在AndroidManifest.xml文件中授权,以及确定API lever为5,

<uses-sdk android:minSdkVersion=”5” />

<uses-permission android:name=”android.permission.MANAGE_ACCOUNTS/>

<uses-permission android:name=”android.permission.ACCOUNT_MANAGER/>

<uses-permission android:name=”android.permission.GET_ACCOUNTS/>

<uses-permission android:name=”android.permission.AUTHENTICATE_ACCOUNTS/>

2. 在Activity中,得到AccountManager对象

AccountManager accountManager = AccountManager.get(this);

AccountManager中的常用方法

addAccount,

addOnAccountsUpdatedListener,

removeOnAccountsUpdatedListener,

clearPassword,

getAccounts,

getAccountsByType,

getPassword,

getUserData,

setPassword,

removeAccount,

将指定类型的帐户信息全部列出来

Account[] accounts = accountManager.getAccountsByType(xxx);

for(Account account : accounts) {

  String name = account.name;

  String type = account.type;

}

如何将帐户信息添加到帐户管理器中

Activity self = this;

String server, username, password, type;

Account account = new Account(name, type);

Bundle userdata = new Bundle();

userdata.putString(“server”, server);

AccountManager am = AccountManager.get(self);

// 向帐户管理器中添加一个帐户

if(am.addAccountExplicitly(account, password, userdata)) {

  Bundle result = new Bundle();

  result.putString(AccountManager.KEY_ACCOUNT_NAME, username);

  result.putString(AccountManager.KEY_ACCOUNT_TYPE, type);

  setAccountAuthenticatorResult(result);

}

// 添加一个帐户服务(Service)和一个验证器(AbstractAccountAuthenticator)

1. 构建res/xml/authenticator.xml

<?xml version=”1.0” encoding=”utf-8”?>

<account-authenticator xmlns:android=”http://schemas.android.com/apk/res/android”

  android:accountType=”com.txrj.AccountType”

  android:icon=”@drawable/icon”

  android:smallIcon=”@drawable/icon”

  android:label=”@string/account_label”

  android:accountPreferences=”@xml/account_preferences”

/>

2. 在AndroidManifest.xml文件中开启一个帐户管理服务

<service android:name=”SleepyAccountsService”>

  <intent-filter>

    <action android:name=”android.accounts.AccountAuthenticator” />

  </intent-filter>

  <meta-data android:name=”android.accounts.AccountAuthenticator”

    android:resource=”@xml/authenticator” />

</service>

3. 实现帐户服务类SleepyAccountsService

public class SleepyAccountsService extends Service {

  private SleepyAccountAuthenticator authenticator;

  public Ibinder onBind(Intent intent) {

    if(intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) {

      return getSleepyAuthenticator().getIBinder();

    return null;

  }

  private SleepyAccountAuthenticator getSleepyAuthenticator() {

    if(authenticator == null)

      authenticator = new SleepyAccountAuthenticator(this);

    return authenticator;

  } 

  }

}

4. 在添加、操作帐户时会通过AbstractAccountAuthenticator实现异步调用。

public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,

    String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException

{

    Bundle bundle = new Bundle();

    Intent intent = new Intent(context, SleepyAccountAuthenticatorActivity.class);;

    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);

    bundle.putParcelable(AccountManager.KEY_INTENT, intent);

    return bundle;

}

Android开发之帐户管理的更多相关文章

  1. Android学习探索之Java 8 在Android 开发中的应用

    前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...

  2. Android 开发一定要看的15个实战项目

    前言: 虽说网上有太多的Android课程,但是大多都是视频,有Android在线开发环境的几乎没有,但是对于学习Android的人来说拥有在线的Android开发环境是非常好的,可以随时动手操作学习 ...

  3. Android开发学习之路-关于Exception

    Exception在Java中是表示异常的一个类.它是Throwable的子类. 而Exception的子类RuntimeException是一个特殊的异常类,在代码中不需要对此类进行throw,而是 ...

  4. Android开发学习之路-Android中使用RxJava

    RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...

  5. Android开发学习之路-记一次CSDN公开课

    今天的CSDN公开课Android事件处理重难点快速掌握中老师讲到一个概念我觉得不正确. 原话是这样的:点击事件可以通过事件监听和回调两种方法实现. 我一听到之后我的表情是这样的: 这跟我学的看的都不 ...

  6. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  7. Android开发-之监听button点击事件

    一.实现button点击事件的方法 实现button点击事件的监听方法有很多种,这里总结了常用的四种方法: 1.匿名内部类 2.外部类(独立类) 3.实现OnClickListener接口 4.添加X ...

  8. Android 开发环境在 Windows7 下的部署安装

    Android SDK Android SDK 为 Android 应用的开发.测试和调试提了必要的API库和开发工具. ADT Bundle 下载 如果你是一个android 开发新手,推荐你下载使 ...

  9. Android开发之自定义的ListView(UITableViewController)

    Android开发中的ListView, 顾名方法思义,就是表视图.表示图在iOS开发中就是TableView.两者虽然名称不一样,但是其使用方法,使用场景以及该控件的功能都极为相似,都是用来展示大量 ...

随机推荐

  1. PHP -- 页面传值的6种获取方法

    1.PHP4以后获取传值的方法 一般在页面中传值常见的是POST.GET和COOKIE几种,所以下面我也主要介绍这几种.PHP4以后都采用的是$_POST.$_GET等数组来获取网页传值.在PHP3. ...

  2. WebDriver工作原理

    http://www.cnblogs.com/timsheng/archive/2012/06/12/2546957.html 通过研究selenium-webdriver的源码,笔者发现其实webd ...

  3. 创建Windows窗体 : WinMain() 与 WndProc()

    #include <windows.h> #include <mmsystem.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, ...

  4. 三款工作流引擎比较:WWF、netBPM 和 ccflow

    下面将对目前比较主流的三款工作流进行介绍和比较,然后通过三款流程引擎分别设计一个较典型的流程来给大家分别演示这三款创建流程的过程.这三款工作流程引擎分别是 Windows Workflow Found ...

  5. 高级需求分析UML建模设计模式笔记

    1.REQ->HLR 分析 全系统性质->AD设计 Context,BOM,Conception 2.REQ->LLR 分析 模块分析->DD设计 + 编码 Feature,B ...

  6. [翻译] JFDepthView 给view提供3D景深

    JFDepthView 给view提供3D景深 https://github.com/atljeremy/JFDepthView This is an iOS project for presenti ...

  7. zend studio配置调试(Xdebug方式)

    1.下载xdebug http://xdebug.org/download.php 我下的是PHP 5.4 VC9 (32 bit) [当前系统php是php5.4.14(win32)版本] 2.配置 ...

  8. C#调用API向外部程序发送数据

    C#调用API向外部程序发送数据 最近有可能要做一个项目.在项目中有这么一个功能,在A程序中调用B程序,同时在A程序中进行登陆后,要将A程序的登录名和密码自动填充到B程序的登陆对话框中,这样B程序就不 ...

  9. Matplotlib Tutorial(译)

    Matplotlib Tutorial(译) 翻译自:Matplotlib tutorialNicolas P. Rougier - Euroscipy 2012 toc{: toc} 这个教程基于可 ...

  10. 需要掌握哪些python标准库和三方库?

    讨论参考:https://www.zhihu.com/question/20501628 库太多了,根据需要使用相应领域的三方库:至于对于企业常用的三方库,可以参考热门招聘网站的招聘说明