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;

}

最新文章

  1. ASCII编码对照表
  2. iOS ARC环境下dealloc的使用
  3. Telnet客户端连接服务器,看不见字符,只显示横线
  4. GIT免密码PUSH
  5. DC-DC转换器原理与应用
  6. intent,实现两个活动之间数据的传递
  7. elasticsearch 学习笔记
  8. C++ 函数模板“偏特化”
  9. [转]解决scrapy下载图片时相对路径转绝对路径的问题
  10. 设计模式总结篇系列:桥接模式(Bridge)
  11. JS闭包作用域解析
  12. Tour HDU - 3488 有向环最小权值覆盖 费用流
  13. IDEA创建第一个项目详细过程
  14. codis服务部署前的操作及初始化
  15. 使用脚本实现killproc的功能
  16. [转发]CPU个数、CPU核心数、CPU线程数
  17. 【树哈希】poj1635 Subway tree systems
  18. 33、深入理解Java的接口和抽象类
  19. SweetWeather wpf天气预报源代码
  20. webAPI支持跨域

热门文章

  1. CDOJ 842 天下归晋 树状数组
  2. Hive集合数据类型
  3. java同步ldap实例
  4. 容器+AOP实现动态部署(四)
  5. 显示所有环境变量:env 或者 printenv
  6. Flink及主流流框架spark,storm比较
  7. KB/KiB,MB/MiB,GB/GiB了解一下
  8. LogBack通过MDC实现日志记录区分用户Session
  9. Jni本地多线程回调Java函数,env-&gt;findClass()失败。
  10. Powerdesigner数据库建模--概念模型--ER图