2017-08-10 121 views
1

我在制作电话簿应用程序。选择保存联系人的帐户

我可以看到一些接触都有不同的ACCOUNT_TYPE_AND_DATA_SET(com.whatsapp,com.viber.voip,com.google,com.android.huawei.sim,com.android.huawei.phone等)。

这里有一个问题:我怎样才能得到可用的帐户列表(部门)保存联系人?

回答

0

您可以使用该服务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,你可能也想检查一下。

+0

我可以通过这种方式获得所有帐户。但我可以使用哪些保存联系人?例如,我有3个账户(谷歌,WhatsApp,Viber),但实际上我可以只保存联系人在谷歌账户(也在本地手机存储和SIM卡)。 –

+0

当然,看我的更新 – marmor

+0

非常感谢! –