Account简介

android中的android.accounts.Account代表的是手机的基本账号信息(name和type).

我们可以通过AccountManager取得Android手机的所有账号。
比如:
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccounts();
然而每种 type 的Account支持的 AUTHORITY (比如 ContactsContract.AUTHORITY )并不尽相同。
我们可以通过 ContentResolver.getSyncAdapterTypes() 取得的 SyncAdapterType 来查询每种 type 的Account支持那些 AUTHORITY
示例程序
void   listAccount () {
Context context=HelloActivity.this;
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccounts();
HashSet<String> contactAccountTypes = new HashSet<String>();
SyncAdapterType []  syncs  =  ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
Log.i(tag,"type:"+sync.accountType+" autohrity:"+sync.authority);
if (ContactsContract.AUTHORITY.equals(sync.authority)
&& sync.supportsUploading()) {
contactAccountTypes.add(sync.accountType);
}
}
ArrayList<Account> contactAccounts = new ArrayList<Account>();
for (Account acct : accounts) {
if (contactAccountTypes.contains(acct.type)) {
contactAccounts.add(acct);
}
}
 
for (Account ac : contactAccounts) {
Log.i(tag, "name:" + ac.name + " type:" + ac.type);
}
}

猜你喜欢

转载自huaonline.iteye.com/blog/2029104