Android uses content providers to add contact actions

Source: Internet
Author: User

The "Contact for Android acquisition System" above describes how to get the system contact and the actual combat, this article will explain how to add a contact information

Add a Contact

1. Add the Raw_contacts table and add a contact ID

2. Manipulate the data table, insert the contact information, and each contact data corresponds to a column raw_contact_id

Create a new project with the following layout file code:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <EditTextAndroid:id= "@+id/et_name"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the name of the contact person" />    <EditTextAndroid:id= "@+id/et_phone"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the phone number of the contact person" />    <EditTextAndroid:id= "@+id/et_email"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter the mailbox of the contact person" />    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Save"Android:text= "Submit Data" /></LinearLayout>

Add related code

 Packagecom.wuyudong.addcontact;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.ContentResolver;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_name; PrivateEditText Et_phone; PrivateEditText Et_mail; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Et_name=(EditText) Findviewbyid (r.id.et_name); Et_phone=(EditText) Findviewbyid (R.id.et_phone); Et_mail=(EditText) Findviewbyid (R.id.et_email); }     Public voidSave (View view) {String name=Et_name.gettext (). toString (). Trim (); String Phone=Et_phone.gettext (). toString (). Trim (); String Email=Et_mail.gettext (). toString (). Trim (); if(Textutils.isempty (name) | |textutils.isempty (phone)||textutils.isempty (email)) {Toast.maketext ( This, "Content cannot be empty", 0). Show (); } Else {            //Save DataContentresolver resolver =Getcontentresolver (); Uri URI= Uri.parse ("Content://com.android.contacts/raw_contacts"); Uri Datauri= Uri.parse ("Content://com.android.contacts/data"); Cursor Cursor= Resolver.query (URI,NULL,NULL,NULL,NULL); intCount =Cursor.getcount (); Contentvalues Values=Newcontentvalues (); intcontact_id = count + 1; Values.put ("CONTACT_ID", contact_id);            Resolver.insert (URI, values); //2. Insert the specific data into the data table, type, data1 specific dataContentvalues Namevalue =Newcontentvalues (); Namevalue.put ("MimeType", "vnd.android.cursor.item/name");//Specifying data TypesNamevalue.put ("Data1", name); Namevalue.put ("raw_contact_id", contact_id);//Be sure to remember which contact the specified data belongs to.Resolver.insert (Datauri, namevalue); Contentvalues Emailvalue=Newcontentvalues (); Emailvalue.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2"); Emailvalue.put ("Data1", email); Emailvalue.put ("RAW_CONTACT_ID", contact_id);            Resolver.insert (Datauri, Emailvalue); Contentvalues Phonevalue=Newcontentvalues (); Phonevalue.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2"); Phonevalue.put ("Data1", phone); Phonevalue.put ("RAW_CONTACT_ID", contact_id);                        Resolver.insert (Datauri, Phonevalue); Toast.maketext ( This, "Add Success", 0). Show (); }    }}

Android uses content providers to add contact actions

Related Article

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.