Android-contentprovider reading and inserting phone contacts

Source: Internet
Author: User

Target effect:

First in the simulator casually add two contacts, run the program, and then open the contact, found more contacts "Zhang San", and the log output three to Anxi person's name and mobile phone number and number type. (Run multiple times, so the ID inserted is not 3)


The 1.activity_main.xml page is temporarily unavailable, so there is no need to change it.


2.mainactivity.java pages to write queries and add methods.

Mainactivity.java page:

Package Com.example.contentprovider;import Android.net.uri;import Android.os.bundle;import Android.provider.contactscontract;import Android.provider.contactscontract.commondatakinds;import Android.provider.contactscontract.commondatakinds.email;import Android.provider.contactscontract.commondatakinds.phone;import Android.provider.contactscontract.commondatakinds.structuredname;import Android.provider.contactscontract.contacts;import Android.provider.contactscontract.data;import Android.provider.contactscontract.rawcontacts;import Android.app.activity;import Android.content.ContentResolver ; Import Android.content.contenturis;import Android.content.contentvalues;import Android.database.cursor;import Android.util.log;import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);/* insert */insert ();/* query */select ();} /* Query */private void Select () {ContentresolvEr cr=getcontentresolver ();/* The first parameter is a URI, the second parameter is the query for which columns, the third parameter is the query condition, the fifth parameter is the Sort method *//* the query ID and the name */cursor c=cr.query ( Contacts.content_uri,new string[]{contacts._id,contacts.display_name},null,null,null); if (C!=null) {while ( C.movetonext ()) {int Id=c.getint (C.getcolumnindex ("_id")); String name=c.getstring (C.getcolumnindex ("display_name")); LOG.I ("Mainactivity", "_id" +id); LOG.I ("Mainactivity", "Display_name" +name);/* Check contact's phone number by ID */cursor everyname=cr.query (phone.content_uri,new string[]{phone.number,phone.type},phone.contact_id+ "=" +id,null,null); if (Everyname!=null) {while ( Everyname.movetonext ()) {/* Query phone number type, type 0 for landline phone, type 1 for mobile phone */int Type=everyname.getint ( Everyname.getcolumnindex (Phone.type)); if (type==phone.type_home) {log.i ("mainactivity", "landline phone" + Everyname.getstring (Everyname.getcolumnindex (Phone.number)));} else if (type==phone.type_mobile) {log.i ("mainactivity", "Mobile phone" +everyname.getstring (Everyname.getcolumnindex ( Phone.number)));}} Everyname.close ();} /* Query the contact's email address based on ID */cursor everyemail=cr.query (email.contenT_uri,new string[]{email.data,email.type},email.contact_id+ "=" +id,null,null), if (Everyemail!=null) {while ( Everyemail.movetonext ()) {int Type=everyemail.getint (Everyemail.getcolumnindex (Email.type)), if (type==Email.TYPE_ Work) {log.i ("mainactivity", "Working Mailbox" +everyemail.getstring (Everyemail.getcolumnindex (Email.data)));}} Everyemail.close ();}} C.close ();}} /* Insert */private void Insert () {contentresolver cr=getcontentresolver ();/* Insert a row of data into the contact */contentvalues values=new Contentvalues (); Uri Uri=cr.insert (Rawcontacts.content_uri, values); Long Raw_contact_id=contenturis.parseid (URI); values.clear ();//Insert name Values.put (structuredname.raw_contact_id,raw_ CONTACT_ID); Values.put (Structuredname.display_name, "Zhang San"); Values.put (Structuredname.mimetype, Structuredname.content_item_type); Uri=cr.insert (data.content_uri,values);//Insert Telephone information values.clear (); Values.put ( PHONE.RAW_CONTACT_ID,RAW_CONTACT_ID); Values.put (Phone.number, "13333333333");//Add number Values.put (Phone.mimetype, Phone.content_item_type); Values.put (Phone.type,phone.type_mobILE);//Add Number type Uri=cr.insert (Data.content_uri, values);}} 


3. The operation contact needs to obtain the permission, therefore in the Androidmainfest.xml page in the Permissions column to add.

4. The program runs to show the target effect.








Android-contentprovider reading and inserting phone contacts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.