First, we need to know the two URIs:
1,uri uri = uri.parse ("content://com.android.contacts/raw_contacts"); Check the data table raw_contact_id
2,Uri uri1 = Uri.parse ("Content://com.android.contacts/data");
To get the information inside the database, you have to know the structure of the table. Let's look at the view_data table:
We only need to be concerned with the following three columns of data:
From the table we can see that Google placed contact information in a column, that is, data1 column, according to RAW_CONTACT_ID to distinguish whether the same person's information, according to MimeType distinguish the contact information in the name, phone, email, So the first thing we want to get is raw_contact_id, but the raw_contact_id in the View_data table is repetitive.
Now let's look at a table raw_contacts:
From this table, we can get the contact's raw_contact_id, we can get the number of contacts, and then get each contact through raw_contact_id.
The structure of the table is understood, let's look at the code:
1 Public voidreadcontacts () {2String name =NULL;3String phone=NULL;4String email=NULL;5Uri uri = uri.parse ("Content://com.android.contacts/raw_contacts");6Uri Uri1 = Uri.parse ("Content://com.android.contacts/data");7cursor cursor = getcontentresolver (). Query (URI,Newstring[]{"contact_id"},NULL,NULL,NULL);8 while(Cursor.movetonext ()) {9String contacts_id = cursor.getstring (0);Ten //System.out.println ("contact_id:" +contacts_id); One A if(contacts_id!=NULL){ - /** - * Projection The second parameter, the column to be queried, or null to query all columns the * Selection The third parameter, according to what conditions query - * Selectionargs Fourth parameter, according to what to check the parameters - * - */ +Cursor Cursor1 = Getcontentresolver (). Query (Uri1,Newstring[]{"Data1", "MimeType"}, "Raw_contact_id=?",NewSTRING[]{CONTACTS_ID},NULL); - while(Cursor1.movetonext ()) { +String data1 = cursor1.getstring (0); AString mimetype = cursor1.getstring (1); at //System.out.println ("data1:" +data1+ "--" + "MimeType:" +mimetype); - if(Mimetype.equals ("Vnd.android.cursor.item/name")){ -Name =data1; -}Else if(Mimetype.equals ("Vnd.android.cursor.item/phone_v2")){ -Phone =data1; -}Else if(Mimetype.equals ("Vnd.android.cursor.item/email_v2")){ inEMail =data1; - } to + } -System.out.println ("Name:" +name+ "-Phone:" +phone+ "-email:" +eMail); the } * } $}
Remember to add permissions:
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
From the code, we can see very clearly, first get raw_contact_id, then through him, get data1 and mimetype, and then through MimeType know the data in Data1 is name, phone or email.
PS: Actually parsing data table is parsing view_data
Getcontentresolver () Content resolver query contact