Android Question How do I get Get the Google Contact Email for a Contact

MrKim

Well-Known Member
Licensed User
Longtime User
When adding a contact in Android, if you have multiple Google email accounts you can choose which contact list to back that Contact up to.

II want to retrieve the account to which each contact has been tied.
I am looking for the information circled in red below.

Thanks for any help.

upload_2016-7-18_20-28-46.png
 

MrKim

Well-Known Member
Licensed User
Longtime User
ContactsUtils doesn't return this field. I tried looking for the relevant field in the documentation and didn't find it: https://developer.android.com/reference/android/provider/ContactsContract.DataColumns.html

If you do find it then I can help you port it to ContactsUtils.
ContactsUtils doesn't return this field. I tried looking for the relevant field in the documentation and didn't find it: https://developer.android.com/reference/android/provider/ContactsContract.DataColumns.html

If you do find it then I can help you port it to ContactsUtils.

It appears to be in ContactsContract.SyncColumns ACCOUNT_NAME andACCOUNT_TYPE?
https://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html
OR
ContactsContract.RawContacts ACCOUNT_NAME and ACCOUNT_TYPE
https://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add this sub to ContactsUtils:
B4X:
'Returns a Map. The keys are the account names and the values are the account types.
Public Sub GetAccounts(Id As Long) As Map
   Dim uri As Uri
   uri.Parse("content://com.android.contacts/contacts/" & Id & "/entities")
   Dim c As Cursor = cr.Query(uri, Array As String("account_name", "account_type"), "", Null, "")
   Dim m As Map
   m.Initialize
   For i = 0 To c.RowCount - 1
     c.Position = i
     m.Put(c.GetString("account_name"), c.GetString("account_type"))
   Next
   c.Close
   Return m
End Sub
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Add this sub to ContactsUtils:
B4X:
'Returns a Map. The keys are the account names and the values are the account types.
Public Sub GetAccounts(Id As Long) As Map
   Dim uri As Uri
   uri.Parse("content://com.android.contacts/contacts/" & Id & "/entities")
   Dim c As Cursor = cr.Query(uri, Array As String("account_name", "account_type"), "", Null, "")
   Dim m As Map
   m.Initialize
   For i = 0 To c.RowCount - 1
     c.Position = i
     m.Put(c.GetString("account_name"), c.GetString("account_type"))
   Next
   c.Close
   Return m
End Sub

Thanks Erel, this will be useful as well, your code appears to be a list of all available accounts. Fortunately it pointed me in the right direction.
I modified one of the other routines and was able to get what I needed. Here is the code:
B4X:
Public Sub GetAccounts2(Id As Long) As String
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("account_name", "account_type"), "_id = ?", Array As String(Id), "")
    If crsr.RowCount > 0 Then
        crsr.Position = 0
        'ToastMessageShow(crsr.RowCount, False)
        Dim Acct As String = crsr.GetString("account_name") & " Type: " & crsr.GetString("account_type")
    Else
        Acct = "None!"
    End If
    crsr.Close
    Return Acct
End Sub

For those as slow as me this (and Erel's funcition above were added to the ContactUtils Module of the ContactUtils example.

I then modified Main as below:

B4X:
Sub lstContacts_ItemClick (Position As Int, Value As Object)
    Dim c As cuContact = Value
    Dim bmp As Bitmap = cu.GetPhoto(c.Id)
    If bmp.IsInitialized Then ImageView1.SetBackgroundImage(bmp) Else ImageView1.SetBackgroundImage(Null)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(c.DisplayName).Append(CRLF).Append("Note: ").Append(cu.GetNote(c.Id)).Append(CRLF)
    sb.Append("Starred: ").Append(cu.GetStarred(c.Id)).Append(CRLF)
    For Each phone As cuPhone In cu.GetPhones(c.Id)
        sb.Append(phone.Number & ", " & phone.PhoneType).Append(CRLF)
    Next
    For Each email As cuEmail In cu.GetEmails(c.Id)
        sb.Append(email.email).Append(", ").Append(email.EmailType).Append(CRLF)
    Next
    sb.Append("Provider: ").Append(cu.GetAccounts2(c.Id)).Append(CRLF)
    EditText1.Text = sb.ToString
End Sub
Adding the line
sb.Append("Provider: ").Append(cu.GetAccounts2(c.Id)).Append(CRLF)

Note I am not yet using GetAccounts
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Thanks Erel, this will be useful as well, your code appears to be a list of all available accounts. Fortunately it pointed me in the right direction.
I modified one of the other routines and was able to get what I needed. Here is the code:
B4X:
Public Sub GetAccounts2(Id As Long) As String
    Dim crsr As Cursor = cr.Query(dataUri, Array As String("account_name", "account_type"), "_id = ?", Array As String(Id), "")
    If crsr.RowCount > 0 Then
        crsr.Position = 0
        'ToastMessageShow(crsr.RowCount, False)
        Dim Acct As String = crsr.GetString("account_name") & " Type: " & crsr.GetString("account_type")
    Else
        Acct = "None!"
    End If
    crsr.Close
    Return Acct
End Sub

For those as slow as me this (and Erel's funcition above were added to the ContactUtils Module of the ContactUtils example.

I then modified Main as below:

B4X:
Sub lstContacts_ItemClick (Position As Int, Value As Object)
    Dim c As cuContact = Value
    Dim bmp As Bitmap = cu.GetPhoto(c.Id)
    If bmp.IsInitialized Then ImageView1.SetBackgroundImage(bmp) Else ImageView1.SetBackgroundImage(Null)
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append(c.DisplayName).Append(CRLF).Append("Note: ").Append(cu.GetNote(c.Id)).Append(CRLF)
    sb.Append("Starred: ").Append(cu.GetStarred(c.Id)).Append(CRLF)
    For Each phone As cuPhone In cu.GetPhones(c.Id)
        sb.Append(phone.Number & ", " & phone.PhoneType).Append(CRLF)
    Next
    For Each email As cuEmail In cu.GetEmails(c.Id)
        sb.Append(email.email).Append(", ").Append(email.EmailType).Append(CRLF)
    Next
    sb.Append("Provider: ").Append(cu.GetAccounts2(c.Id)).Append(CRLF)
    EditText1.Text = sb.ToString
End Sub
Adding the line
sb.Append("Provider: ").Append(cu.GetAccounts2(c.Id)).Append(CRLF)

Note I am not yet using GetAccounts

Well, it turns out this is not reliable either. Some of the time it returns the account, and sometimes it doesn't.
I am baffled.

And this begs the next question, which is how to add a new contact and connect it to an account. But that is fodder for another thread.
 
Upvote 0
Top