PackageCom.org.demo.demo;ImportCOM.ORG.WANGFENG.R;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classGetphoneactivityextendsActivity {PrivateEditText EditText; Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (r.layout.getphoneactivity); EditText=(EditText) Findviewbyid (R.id.et_getphone); Button=(Button) Findviewbyid (R.id.bb_getphone); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubIntent Intent =NewIntent (getphoneactivity. This, contactactivity.class); Startactivityforresult (Intent,1); } }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { //System.out.println ("ResultCode:" + ResultCode); //System.out.println ("Requestcode:" + requestcode);LOG.D ("Jiejie", "called"); if(ResultCode = =ACTIVITY.RESULT_OK) {String phone= Data.getstringextra ("Phone"); LOG.D ("Jiejie", "_______________" +phone); Phone= Phone.replaceall ("-", ""). ReplaceAll ("", "");//Replace-and spacesedittext.settext (phone);//set the phone number to the input box } Super. Onactivityresult (Requestcode, ResultCode, data); }}
PackageCom.org.demo.demo;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportCOM.ORG.WANGFENG.R;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.TextView; Public classContactactivityextendsActivity {PrivateListView lvlist; PrivateArraylistreadcontact; PrivateTextView back; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_contact); back=(TextView) Findviewbyid (r.id.back); Back.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stubfinish (); } }); Lvlist=(ListView) Findviewbyid (r.id.lv_list); Readcontact=readcontact (); //System.out.println (readcontact);Lvlist.setadapter (NewSimpleadapter ( This, Readcontact, R.layout.contact_list_item,NewString[] {"Name", "Phone" }, New int[] {r.id.tv_name, r.id.tv_phone}); Lvlist.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {String phone= Readcontact.get (position). Get ("phone");//read the phone number of the current itemIntent Intent =NewIntent (); Intent.putextra ("Phone", phone); Setresult (ACTIVITY.RESULT_OK, intent);//put data in intent back to previous pagefinish (); } }); } PrivateArraylistreadcontact () {//First, read the contact's ID ("contact_id") from the Raw_contacts//second, the corresponding phone number and contact name are queried from the data table according to CONTACT_ID//then, according to MimeType to distinguish which is the contact, which is the phone numberUri Rawcontactsuri =Uri. Parse ("Content://com.android.contacts/raw_contacts"); Uri Datauri= Uri.parse ("Content://com.android.contacts/data"); ArrayListNewArraylist(); //read the contact's ID from raw_contacts ("contact_id")Cursor Rawcontactscursor =getcontentresolver (). Query (Rawcontactsuri,NewString[] {"contact_id"},NULL,NULL,NULL); if(Rawcontactscursor! =NULL) { while(Rawcontactscursor.movetonext ()) {String contactId= rawcontactscursor.getstring (0); //System.out.println (contactId); //according to CONTACT_ID, the corresponding phone number and contact name are queried from the data table, in fact the view is queried View_dataCursor Datacursor =getcontentresolver (). Query (Datauri,NewString[] {"Data1", "MimeType"}, "Contact_id=?", NewString[] {contactId},NULL); if(Datacursor! =NULL) {HashMap<string, string> map =NewHashmap<string, string>(); while(Datacursor.movetonext ()) {String data1= datacursor.getstring (0); String mimetype= datacursor.getstring (1); //System.out.println (contactId + ";" + Data1 + ";" //+ mimetype); if("Vnd.android.cursor.item/phone_v2". Equals (MimeType)) {Map.put ("Phone", data1); } Else if("Vnd.android.cursor.item/name". Equals (MimeType)) {Map.put ("Name", data1); }} list.add (map); Datacursor.close (); }} rawcontactscursor.close (); } returnlist; }}
Get phone Contacts