android 帐户管理,Android开发之帐户管理

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

AccountManagerCallback,

AccountManagerFuture,

OnAccountsUpdateListener,

AbstractAccountAuthenticator,

Account,

AccountAuthenticatorActivity,

AccountAuthenticatorResponse,

AccountManager,

AuthenticatorDescription,

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

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

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

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文件中开启一个帐户管理服务

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

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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值