1
我在製作電話簿應用程序。選擇保存聯繫人的帳戶
我可以看到一些接觸都有不同的ACCOUNT_TYPE_AND_DATA_SET(com.whatsapp,com.viber.voip,com.google,com.android.huawei.sim,com.android.huawei.phone等)。
這裏有一個問題:我怎樣才能得到可用的帳戶列表(部門)保存聯繫人?
我在製作電話簿應用程序。選擇保存聯繫人的帳戶
我可以看到一些接觸都有不同的ACCOUNT_TYPE_AND_DATA_SET(com.whatsapp,com.viber.voip,com.google,com.android.huawei.sim,com.android.huawei.phone等)。
這裏有一個問題:我怎樣才能得到可用的帳戶列表(部門)保存聯繫人?
您可以使用該服務AccountManager
:
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
Log.d(TAG, account.type + "/" + account.name);
}
注意,需要GET_ACCOUNTS
許可,如果您指定的Android米及以上,你會ALSE需要詢問用戶通過此權限Runtime Permissions
模型。
UPDATE
要跳過不支持觸點可言,或做支撐觸點,但爲只讀(如Viber的WhatsApp的和)的所有帳戶:
final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
Log.d(TAG, "found SyncAdapter: " + sync.accountType);
if (ContactsContract.AUTHORITY.equals(sync.authority)) {
Log.d(TAG, "found SyncAdapter that supports contacts: " + sync.accountType);
if (sync.supportsUploading()) {
Log.d(TAG, "found SyncAdapter that supports contacts and is not read-only: " + sync.accountType);
// we'll now get a list of all accounts under that accountType:
Account[] accounts = AccountManager.get(this).getAccountsByType(sync.accountType);
for (Account account : accounts) {
Log.d(TAG, account.type + "/" + account.name);
}
}
}
}
歡迎瀏覽SyncAdapterType
中的其他好東西,比如isUserVisible
,你可能也想檢查一下。
我可以通過這種方式獲得所有帳戶。但我可以使用哪些保存聯繫人?例如,我有3個賬戶(谷歌,WhatsApp,Viber),但實際上我可以只保存聯繫人在谷歌賬戶(也在本地手機存儲和SIM卡)。 –
當然,看我的更新 – marmor
非常感謝! –