In the last section we learned how to read system text messages and insert a text message into the system. In this section we learn how to get the system contacts and insert a contact.
Good. Nonsense not much to say, direct operation.
First, like reading a text message, find the location of the contact in the database first.
Then export to view the contents of the database
is how to find the database to hit the system's contact data, (looks like the picture is a bit chaotic, I hope to understand it)
Now that you know how to find the data in the system's database. So let's start writing code implementations:
public class Readcontactactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO A Uto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_readcontact); }public void Readcontact (View v) {contentresolver cr = Getcontentresolver ();/* * First go to raw_contacts table to get contact_id * content:/ /com.android.contacts/raw_contacts is Raw_contacts's interview URL * */cursor contactId = cr.query (Uri.parse ("content:// Com.android.contacts/raw_contacts "), New string[]{" contact_id "}, NULL, NULL, NULL), while (Contactid.movetonext ()) {/* * Get contact_id then go to the data table to get mimetype_id and data1 from raw_contact_id * */string id = contactid.getstring ( Contactid.getcolumnindex ("contact_id"));/** * "Content://com.android.contacts/data" is the URL of the Access data table * by ID that is contact_ ID go to the data table, Data1, raw_contact_id, mimetype_id. * However, the ID is raw_contact_id, so there is no need to find it.And mimetype_id because the Android system has optimized some of our own details. When you find mimetype_id in the data table, you can't find it. * The Android system directly transforms mimetype_id to mimetype */cursor contactdata = Cr.query (Uri.parse ("content ://com.android.contacts/data "), New string[]{" Data1 "," MimeType "}," raw_contact_id =? ", new String[]{id}, NULL); string email = null, phone = NULL, name = Null;while (Contactdata.movetonext ()) {String data1 = contactdata.getstring (Contac Tdata.getcolumnindex ("data1")); String mimetype = contactdata.getstring (Contactdata.getcolumnindex ("mimetype"));/* Assuming MimeType is mailbox */if (" Vnd.android.cursor.item/email_v2 ". Equals (mimetype)) {email = data1;} Suppose MimeType is the phone else if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {phone = data1;} Suppose MimeType is the name of the else if ("Vnd.android.cursor.item/name". Equals (MimeType)) {name = Data1;}} LOG.I ("readcontactactivity", id + '; ' + name + '; "+ phone +"; "+ email);}}}
Printing results are:
is how to get the action of the contact person.
Note: Reading contacts, text messages and other information are required to add permissions:
Now that you know how to get contacts from your system. Then it should be a record of a contact being inserted into the system.
Next we implement inserting a contact record for the system
Insert code for activity:
public void Insertcontact (View v) {contentresolver cr = Getcontentresolver (); cursor cursor = cr.query (Uri.parse ("Content://com.android.contacts/raw_contacts"), New string[]{"_id"}, NULL, NULL, NULL);/* Assuming there is no data in the database, insert the first data */int contactId = 1;if (Cursor.movetolast ()) {/* Assuming there is data, insert to the latest write a */int id = cursor.getint (Cursor.getcolumnindex ("_id")); contactId = id + 1;} Contentvalues values = new Contentvalues ();/* Inserts a new ID into the raw_contacts table */values.put ("contact_id", contactId); Cr.insert ( Uri.parse ("Content://com.android.contacts/raw_contacts"), values);/* Insert name */values.clear (); Values.put ("Data1", " Erlangsheng "), Values.put (" MimeType "," Vnd.android.cursor.item/name "), Values.put (" raw_contact_id ", contactId); Cr.insert (Uri.parse ("Content://com.android.contacts/data"), values);/* Insert Phone */values.clear (); Values.put ("Data1", " 00189890 "), Values.put (" MimeType "," Vnd.android.cursor.item/phone_v2 "), Values.put (" raw_contact_id ", contactId); Cr.insert (Uri.parse ("Content://com.android.contacts/data"), values);}}
The result is:
The above is to insert a record to the contact.
Android Four components Learning ContentProvider four