android 帐户管理,Android开发之帐户管理
android.accounts主要包括了集中式的帳戶管理API,
AccountManagerCallback,
AccountManagerFuture,
OnAccountsUpdateListener,
AbstractAccountAuthenticator,
Account,
AccountAuthenticatorActivity,
AccountAuthenticatorResponse,
AccountManager,
AuthenticatorDescription,
示例學習:添加多個帳戶來集中管理
1. 在AndroidManifest.xml文件中授權(quán),以及確定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);
}
// 添加一個帳戶服務(wù)(Service)和一個驗證器(AbstractAccountAuthenticator)
1. 構(gòu)建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文件中開啟一個帳戶管理服務(wù)
android:resource=”@xml/authenticator” />
3. 實現(xiàn)帳戶服務(wù)類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實現(xiàn)異步調(diào)用。
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;
}
總結(jié)
以上是生活随笔為你收集整理的android 帐户管理,Android开发之帐户管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android模拟器后台截屏,【Andr
- 下一篇: 常见的传销陷阱有哪些?不贪心就是最好的防