Account简介

本文介绍如何使用Android中的AccountManager获取手机上的所有账号信息,并通过ContentResolver查询不同类型的Account支持的AUTHORITY,包括示例代码和所需权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}
}
注意,需要android.permission.GET_ACCOUNTS权限。
运行打印的日志如下:
03-01 14:29:13.422: I/robin(32622): type:com.htc.htctwitter autohrity:com.htc.chirp.provider.Tweet
03-01 14:29:13.422: I/robin(32622): type:com.twitter.android.auth.login autohrity:com.twitter.android.provider.TwitterProvider
03-01 14:29:13.432: I/robin(32622): type:com.htc.cs autohrity:com.htc.connectedservice.csprovider
03-01 14:29:13.432: I/robin(32622): type:com.htc.android.mail.eas autohrity:htceas
03-01 14:29:13.432: I/robin(32622): type:com.htc.htctwitter autohrity:com.htc.htctwitter.Users
03-01 14:29:13.432: I/robin(32622): type:com.htc.android.Stock autohrity:stock
03-01 14:29:13.432: I/robin(32622): type:com.htc.newsreader autohrity:com.htc.googlereader
03-01 14:29:13.432: I/robin(32622): type:com.htc.socialnetwork.facebook autohrity:com.htc.socialnetwork.facebook
03-01 14:29:13.432: I/robin(32622): type:com.htc.socialnetwork.flickr autohrity:com.htc.socialnetwork.flickr.provider.StreamProvider
03-01 14:29:13.432: I/robin(32622): type:com.google autohrity:com.android.contacts
03-01 14:29:13.432: I/robin(32622): type:com.htc.sync.provider.weather autohrity:com.htc.sync.provider.weather
03-01 14:29:13.432: I/robin(32622): type:com.google autohrity:gmail-ls
03-01 14:29:13.432: I/robin(32622): type:com.skype.contacts.sync autohrity:com.android.contacts
03-01 14:29:13.432: I/robin(32622): type:com.google autohrity:subscribedfeeds
03-01 14:29:13.432: I/robin(32622): type:com.htc.socialnetwork.facebook autohrity:com.android.contacts
03-01 14:29:13.432: I/robin(32622): type:com.twitter.android.auth.login autohrity:com.android.contacts
03-01 14:29:13.432: I/robin(32622): type:com.htc.android.mail autohrity:mail
03-01 14:29:13.432: I/robin(32622): type:com.htc.socialnetwork.flickr autohrity:com.android.contacts
03-01 14:29:13.432: I/robin(32622): type:com.htc.cs autohrity:com.htc.wdm.provider.WDMProvider
03-01 14:29:13.432: I/robin(32622): type:com.google autohrity:com.android.calendar
03-01 14:29:13.432: I/robin(32622): type:com.facebook.auth.login autohrity:com.android.contacts
03-01 14:29:13.432: I/robin(32622): name:hudashi@gmail.com type:com.google
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值