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 contacts for a system and insert a contact
OK, 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 database of the system, let's start writing the code:
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 access 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 and 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 to access the data table * by ID that is contact_ ID go to the data table, Data1, raw_contact_id, mimetype_id. * But the ID is raw_contact_id, so there's 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 is not found * Android system directly to mimetype_id to find 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"));/* If MimeType is mailbox */if (" Vnd.android.cursor.item/email_v2 ". Equals (mimetype)) {email = data1;} If MimeType is the phone else if ("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {phone = data1;} If 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 need to add permissions:
Now that you know how to get contacts from your system, you should insert a record of a contact for 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);/* If there is no data in the database, then the first data is inserted */int contactId = 1;if (Cursor.movetolast ()) {/* If 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.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four components Learning ContentProvider four