1 Packagecom.example.visit_contacts;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.util.Log;Ten ImportAndroid.view.Menu; One ImportAndroid.view.MenuItem; A ImportAndroid.view.View; - - Public classMainactivityextendsActivity { the - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); - } + Public voidGetContact (View v) { AContentresolver CR =getcontentresolver (); atCursor C = cr.query (Uri.parse ("Content://com.android.contacts/raw_contacts"),Newstring[]{"contact_id"},NULL,NULL,NULL); - while(C.movetonext ()) { - intid = c.getint (0); -Cursor C2 = cr.query (Uri.parse ("Content://com.android.contacts/data"),Newstring[]{"MimeType", "Data1"}, -"Raw_contact_id=?",NewString[]{id+ ""},NULL); -Contact Contact =NewContact (); in while(C2.movetonext ()) { -String type = c2.getstring (0); toString data1 = c2.getstring (1); + if("Vnd.android.cursor.item/name". Equals (Type) { - Contact.setname (data1); the } * if("Vnd.android.cursor.item/phone_v2". Equals (Type) { $ Contact.setnumber (data1);Panax Notoginseng } - if("Vnd.android.cursor.item/email_v2". Equals (Type) { the Contact.setemail (data1); + } ALOG.I ("GetContact", contact.tostring ()); the } + } - } $ $ Public voidaddcontact (View v) { -Contentresolver CR =getcontentresolver (); - //step1:handle raw_contact table; theCursor C = cr.query (Uri.parse ("Content://com.android.contacts/raw_contacts"),Newstring[]{"_id"},NULL,NULL,NULL); - c.movetolast ();Wuyi intid = c.getint (0); the intinsert_id = ID + 1; - WuContentvalues CV =Newcontentvalues (); -Cv.put ("_id", insert_id); AboutCv.put ("contact_id", insert_id); $Cr.insert (Uri.parse ("Content://com.android.contacts/raw_contacts"), CV); - - //STEP2. Handle Data Table - //Insert Name A cv.clear (); +Cv.put ("_id", 9); theCv.put ("MimeType", "Vnd.android.cursor.item/name"); -Cv.put ("raw_contact_id", insert_id); $Cv.put ("Data1", "Zhangsanfeng"); theCr.insert (Uri.parse ("Content://com.android.contacts/data"), CV); the the //Insert Phone_v2 the cv.clear (); -Cv.put ("_id", 10); inCv.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2"); theCv.put ("raw_contact_id", insert_id); theCv.put ("Data1", "13899998888"); AboutCr.insert (Uri.parse ("Content://com.android.contacts/data"), CV); the the //Insert Email the cv.clear (); +Cv.put ("_id", 11); -Cv.put ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2"); theCv.put ("raw_contact_id", insert_id);BayiCv.put ("Data1", "[email protected]"); theCr.insert (Uri.parse ("Content://com.android.contacts/data"), CV); the } - - the}
mainactivity
1 Packagecom.example.visit_contacts;2 3 Public classContact {4 5 PrivateString name;6 PrivateString number;7 PrivateString Email;8 PublicString GetName () {9 returnname;Ten } One Public voidsetName (String name) { A This. Name =name; - } - PublicString GetNumber () { the returnNumber ; - } - Public voidSetnumber (String number) { - This. Number =Number ; + } - PublicString Getemail () { + returnemail; A } at Public voidsetemail (String email) { - This. email =email; - } - PublicContact (string name, string number, string email) { - Super(); - This. Name =name; in This. Number =Number ; - This. email =email; to } + PublicContact () { - Super(); the //TODO auto-generated Constructor stub * } $ @OverridePanax Notoginseng PublicString toString () { - return"Contact [Name=] + name +", number= "+ number +", email= " the+ email + "]"; + } A the +}
Contact
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:paddingbottom= "@dimen/activity_vertical_margin"6android:paddingleft= "@dimen/activity_horizontal_margin"7android:paddingright= "@dimen/activity_horizontal_margin"8android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context= "Com.example.visit_contacts. Mainactivity "Tenandroid:orientation= "Vertical" > One A<TextView -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theandroid:text= "@string/hello_world"/> - -<Button -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" -android:onclick= "GetContact" +android:text= "Get contact information for your system"/> A<Button atAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:text= "New contact information for the system" -android:onclick= "Addcontact" -/> - in</LinearLayout>
Activity_main
009_02 accessing the System contact database.