1. Add a contact logic idea
(1) First create a new ID in raw_contacts
(2) Add this ID to the data table
2. Below is a case that explains how to add a data to a contact:
(1) First of all, we focus on the layout file,Activity_main.xml, as follows:
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.himi.addcontact.MainActivity" >Ten One <Button A Android:onclick= "click" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:layout_centerhorizontal= "true" - android:layout_centervertical= "true" - Android:text= "Add a contact data" /> - + </Relativelayout>
The layout effect is as follows:
(2) followed by mainactivity, as follows:
1 Packagecom.himi.addcontact;2 3 Importandroid.app.Activity;4 ImportAndroid.content.ContentResolver;5 Importandroid.content.ContentValues;6 ImportAndroid.database.Cursor;7 ImportAndroid.net.Uri;8 ImportAndroid.os.Bundle;9 ImportAndroid.view.View;Ten ImportAndroid.widget.Toast; One A Public classMainactivityextendsActivity { - - @Override the protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - } + - //Add a contact information to the system's database + Public voidClick (View view) { AUri uri = uri.parse ("Content://com.android.contacts/raw_contacts"); atUri Datauri = Uri.parse ("Content://com.android.contacts/data"); - //get the parser for ContentProvider -Contentresolver resolver = This. Getcontentresolver (); - //1. Add a new ID to the Raw_contace table _id desc: Indicates descending by ID -cursor cursor = resolver.query (URI,Newstring[]{"_id"},NULL,NULL, "_id desc"); - Cursor.movetofirst (); in int_id = cursor.getint (0);//before, in descending order, this gets the maximum ID value in the table - //Calculate the ID of the newly added entry to intNewId = _id+1; +Contentvalues values =Newcontentvalues (); -Values.put ("contact_id", newId); the Resolver.insert (URI, values); * $ //2. Add the corresponding data in the database tablePanax Notoginseng //Add phone number phone -Contentvalues Phonevalue =Newcontentvalues (); thePhonevalue.put ("raw_contact_id", newId); +Phonevalue.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2"); APhonevalue.put ("Data1", "123456"); the Resolver.insert (Datauri, phonevalue); + //Add Name -Contentvalues Namevalue =Newcontentvalues (); $Namevalue.put ("raw_contact_id", newId); $Namevalue.put ("MimeType", "Vnd.android.cursor.item/name"); -Namevalue.put ("Data1", "Step Cloud"); - Resolver.insert (Datauri, namevalue); the //Add Email -Contentvalues Emailvalue =Newcontentvalues ();WuyiEmailvalue.put ("raw_contact_id", newId); theEmailvalue.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2"); -Emailvalue.put ("Data1", "[email protected]"); Wu Resolver.insert (Datauri, emailvalue); - AboutToast.maketext ( This, "Add Success", 0). Show (); $ } - } -
(3) finally remember to add permissions, as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.himi.addcontact"4 Android:versioncode= "1"5 Android:versionname= "1.0" >6 7 <USES-SDK8 android:minsdkversion= " the"9 android:targetsdkversion= "+" />Ten <uses-permissionAndroid:name= "Android.permission.READ_CONTACTS"/> One <uses-permissionAndroid:name= "Android.permission.WRITE_CONTACTS"/> A - <Application - Android:allowbackup= "true" the Android:icon= "@drawable/ic_launcher" - Android:label= "@string/app_name" - Android:theme= "@style/apptheme" > - <Activity + Android:name=". Mainactivity " - Android:label= "@string/app_name" > + <Intent-filter> A <ActionAndroid:name= "Android.intent.action.MAIN" /> at - <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> - </Intent-filter> - </Activity> - </Application> - in </Manifest>
(4) Deployment of the program to the simulator, as follows:
Let's take a look at the data in the system communication record , as follows:
We click on " Add a contact Data ", as follows:
Android (Java) Learning notes 251:contentprovider using to add data to contacts