Add a type of contact (RawContact), sometimes they are combined with telephone contacts LOOKUP_KEY. It turns out that some contacts entries contain two RawContact'a one integrated phone, one of mine. The question is how to find out whether there is a Contacts RawContract my type? Or learn to RawContacts to which he refers Contacts?
Match the "_id" of the contact to the "contact_id" of the raw contact.
Related
I was just going through the ContactsContract API in Android and I am stuck with the overview part of it.
Then I got this link https://developer.android.com/guide/topics/providers/contacts-provider but I am still having difficulty understanding Contacts in android.
Can any of you provide me with links or explanation what is a Contact is, in Android perspective, for me it is just the number we save on our phone but I now know it is some thing more. Please explain to me (or provide link to a simpler or clearer explanation which is not the android documentation itself) what these three tables contain as I am unable to understand it from the docs.
ContactsContract.Contacts table
ContactsContract.RawContacts table
ContactsContract.Data table
Thanks in advance.
As I wrote in many answers before in various phrasing:
The Contacts DB is organized in three main tables:
Contacts - each entry represents one contact, and groups together one or more RawContacts
RawContacts - each entry represents data about a contact that was synced in by some SyncAdapter (e.g. Whatsapp, Google, Facebook, Viber), this groups multiple Data entries
Data - The actual data about a contact, emails, phones, etc. each line is a single piece of data that belongs to a single RawContact
Usually what happens is that an app (e.g. Google, Whatsapp, Linkedin) that wishes to create a new contact will create a new row in the RawContacts table that will usually contain just a name, and then use that row's _ID to add rows into the Data table for phones, emails, addresses, photos, etc.
Android will then either create a new row in Contacts to assign to that new raw-contact (i.e. a new contact was created), or if it decides that raw-contact contain similar enough information to an existing contact, will have an existing contact row assigned to that new raw-contact (i.e. the new information will be added to an existing contact).
The "assigning" part is done like this - each row in Data has a column RAW_CONTACT_ID which tells the contacts app which raw-contact does this info belong to, and each row in RawContacts has a column CONTACT_ID which tells the contacts app which row in Contacts this raw-contact belong-to.
So to get information on contact with _ID = 1234, you could first query info from Contacts where _ID = 1234, then query more info from RawContacts where CONTACT_ID = 1234, then query for more info from Data where RAW_CONTACT_ID IN (X) where X is the list of raw-contact ids you found previously.
The Data table also has a CONTACT_ID column, so you can basically skip the RawContact query, and get all the data (phones, emails, etc.) directly from the contact-id.
Hope that's clear.
I'm writing a class that handles Android contact information and after some struggle, I've written a function that retrieves all information about the user's contacts, including all phone numbers, email addresses, postal addresses, etc. However, I still don't understand how this information is stored in the phone, so I'm hoping someone can give me some insight on this.
So, a user can have multiple phone numbers, multiple email addresses, etc. For contacts you have ContactsContract.Contacts.CONTENT_URI for the table containing all contacts and for phone numbers you have ContactsContract.CommonDataKinds.Phone.CONTENT_URI. But then there is also ContactsContract.RawContacts.CONTENT_URI, which is supposed to contain all the data for your contacts, I think. My assumption is that in the Contacts table, contact ID is the unique identifier for separate rows, whereas for the Phone table, phone number is the unique row identifier. If RawContacts contains all columns of the ContactsContract classes, then there would be no unique identifier for each row, since each contact might be assigned multiple phone numbers, multiple email addresses, etc. In other words, I don't understand how such a table is structured.
So my question is this: Are the various tables containing contact information--Contacts, Phone, Email, StructuredPostal, etc.--completely separate or is the information for each of those tables extracted from the larger RawContacts table? Or am I misunderstand what RawContacts is? Since the class I am writing will help move contacts from the phone contact list into a separate database, knowing this information will help me understand whether I should store the information in multiple databases or just one (similar to how I described the RawContacts database above).
The android API Guide on Contacts Provider gives a nice description of how the Contacts tables are stored (it's a bit convoluted, allowing the merge of many contacts)
picture of data hierarchy
your Q:
So my question is this: Are the various tables containing contact
information--Contacts, Phone, Email, StructuredPostal,
etc.--completely separate or is the information for each of those
tables extracted from the larger RawContacts table?
Looking over the docs again (I've not coded against these tables, only read docs), the Phone, Email... tables appear to be separate. However, the information looks to be stored in both tables, the contact and also in the RawContacts.
I've got an app in the market which stores details against contacts by using a contact picker. When I've returned from the contact picker I had been using the following to obtain the contact:
cursor.getColumnIndex(ContactsContract.Contacts._ID)
I've found that using this is ok until somebody flashes a new rom or gets a new phone and then all the ID's have changed.
I've looked at the android documentation and I've seen references to using ContactsContract.Contacts.LOOKUP_KEY, but the description confuses me "An opaque value that contains hints on how to find the contact if its row id changed as a result of a sync or aggregation."
So what should I be using to store the contact ID?
Lookup Key is the correct reference for contacts.
Contacts Provider / Contacts:
The ContactsContract.Contacts table also has the column LOOKUP_KEY
that is a "permanent" link to the contact row. Because the Contacts
Provider maintains contacts automatically, it may change a contact
row's _ID value in response to an aggregation or sync. Even If this
happens, the content URI CONTENT_LOOKUP_URI combined with contact's
LOOKUP_KEY will still point to the contact row, so you can use
LOOKUP_KEY to maintain links to "favorite" contacts, and so forth.
This column has its own format that is unrelated to the format of the
_ID column.
I have inserted few contacts in through the android emulator.
I wanted to fetch the names, number,emails,etc. for each contact.
I understood that for fetching contact number we need to refer to
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
instead of
ContactsContract.Contacts.CONTENT_URI
My question is how do i link both the query results so that i can aggregate a single contact and its attributes together ? It seems that only ContactsContract.Data.DISPLAY_NAME is common in both the results and the only attribute which can fetched in both the URI's without specifying in the query's projection.
Can anyone guide me ?
Thanks,
Adithya.
try these links:
How to call Android contacts list?
How to get contacts from native phonebook in android
How to obtain all details of a contact in Android
How to get the first name and last name from Android contacts?
How to import contacts from phonebook to our application
Android contacts extraction
How to get all android contacts but without those which are on SIM
Use select from ContactsContract.Data.CONTENT_URI and group results by LOOKUP_KEY. You'll receive a single cursor with phones, emails, etc., grouped by contact.
In onactivityresult we r getting cursor to the contact database to access various fields of contact database we have ContactsContract.PhoneLookup through which we can get index various db columns and then acccess them.
As you can know there are aggregated contacts (one 1) and each can have multiple raw contacts linked together to that contact (many N)
So there is a 1 contact -> N raw contacts relationship between them.
When you need to store a custom info for your contact, you have to store it for one of the raw contacts. The question is, how do you select which raw contact to use? The raw contacts can belong to different accounts.
The account_name and account_type columns of the raw contact provide information about the account;
that should be sufficient to find the correct contact.