Use contentprovider to insert a contact

Source: Internet
Author: User

First, I declare that this example is an example from instructor Li Gang's crazy android handout. I just tried it again.

1. Declare the layout file first

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <edittext Android: id = "@ + ID/edittext1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: EMS = "10" Android: hint = "Name: "> <requestfocus/> </edittext> <edittext Android: Id =" @ + ID/edittext2 "Android: layout_width =" match_parent "Android: layout_height =" wrap_content "Android: EMS = "10" Android: hint = "Tel:"/> <edittext Android: Id = "@ + ID/edittext3" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: EMS = "10" Android: hint = "Email:"/> <button Android: Id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "insert contact"/> </linearlayout>

2. The main file is as follows:

Package COM. example. democontent; import android. app. activity; import android. content. contenturis; import android. content. contentvalues; import android.net. uri; import android. OS. bundle; import android. provider. contactscontract. commondatakinds. email; import android. provider. contactscontract. commondatakinds. phone; import android. provider. contactscontract. commondatakinds. structuredname; import android. provider. contactscontract. data; import android. provider. contactscontract. rawcontacts; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. toast; public class mainactivity1 extends activity {private edittext et_name; private edittext et_phone; private edittext et_email; private button; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main1); button = (button) findviewbyid (R. id. button1); et_name = (edittext) findviewbyid (R. id. edittext1); et_phone = (edittext) findviewbyid (R. id. edittext2); et_email = (edittext) findviewbyid (R. id. edittext3); button. setonclicklistener (New onclicklistener () {public void onclick (view arg0) {insert () ;}}) ;}public void insert () {string name = et_name.gettext (). tostring (); string email = et_email.gettext (). tostring (); string phone = et_phone.gettext (). tostring (); // create a contentvaluescontentvalues values = new contentvalues (); // send it to rawcontacturi. insert a null value in content_uri to obtain rawcontactiduri rawcontacturi = getcontentresolver (). insert (rawcontacts. content_uri, values); long rawcontactid = contenturis. parseid (rawcontacturi); values. clear (); values. put (data. raw_contact_id, rawcontactid); // sets the content type values. put (data. mimetype, structuredname. content_item_type); // set the contact name values. put (structuredname. given_name, name); // Add the contact name getcontentresolver () to the contact Uri (). insert (Android. provider. contactscontract. data. content_uri, values); values. clear (); values. put (data. raw_contact_id, rawcontactid); // sets the content type values. put (data. mimetype, phone. content_item_type); // set the contact's phone number values. put (phone. number, phone); // Add the phone number getcontentresolver () to the contact phone number Uri (). insert (Android. provider. contactscontract. data. content_uri, values); values. clear (); values. put (data. raw_contact_id, rawcontactid); // sets the content type values. put (data. mimetype, email. content_item_type); // sets the contact address values. put (email. data, email); // set the email type values. put (email. type, email. type_work); // Add the getcontentresolver () email to the contact (). insert (Android. provider. contactscontract. data. content_uri, values); toast. maketext (mainactivity1.this, "Contact added successfully", toast. length_short ). show ();}}

3. Permission issues

    <uses-permission android:name="android.permission.READ_CONTACTS" />    <uses-permission android:name="android.permission.WRITE_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.