2017-08-10 6 views
1

私は電話帳アプリケーションを作成しています。連絡先を保存するアカウントを選択する

私は連絡先の一部が異なるACCOUNT_TYPE_AND_DATA_SET(com.whatsapp、com.viber.voip、com.google、com.android.huawei.sim、com.android.huawei.phoneなど)を持って見ることができます。

ここに質問があります:アカウント(連絡先)を保存するための利用可能なリストの入手方法を教えてください。

あなたはそのための AccountManagerサービスを使用することができます

答えて

0

Account[] accounts = AccountManager.get(this).getAccounts(); 
for (Account account : accounts) { 
    Log.d(TAG, account.type + "/" + account.name); 
} 

注意、あなたは、Android Mをターゲットにし、上記している場合、あなたは経由でこの権限をユーザーに依頼する必要がありalseます、GET_ACCOUNTS許可が必要Runtime Permissionsモデル。で、すべての連絡先をサポートする、またはサポート・コンタクトを行うだけ(ViberのとのWhatsAppのように)読まれていないすべてのアカウントをスキップする

UPDATE

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つのアカウント(google、whatsapp、viber)を持っていますが、実際には私はGoogleアカウント(ローカル電話機のストレージとSIMカード)でも連絡先を保存できます。 –

+0

確かに、私の更新を参照してください – marmor

+0

ありがとう! –

関連する問題