The previous article has implemented the relevant layout, this article then carries on the related function realization
This article address: http://www.cnblogs.com/wuyudong/p/5951794.html, reprint please indicate the source.
Read system contacts
When the "Select Contacts" button is clicked, the contact list is displayed, and the system contacts are read in the following steps:
The system contact provides a content provider, through the content parser, that matches the URL address
1, Content Parser
2,url address, view system contact database, content provider source code
Look at the API document's manifest file, then look at the Java class (contact database has more than one table)
contents://com.android.contacts/Table Name
3, the table structure of the core table in the system contact database
Raw_contacts Contact table: contact_id Contact Uniqueness ID Value
Data User Information table: raw_contact_id as a foreign key, and raw_contacts in contact_id to do related queries
Gets the Data1 field that contains the phone number and contact name
mimetype_id field that contains the data type for the current row data1
Mimetypes Type table: Gets the data table in mimetype_id and mimetypes _id makes the associated query, gets the type of information pointed to
Phone Number: VND.ANDROID.CURSOR.ITEM/PHONE_V2
User name: Vnd.android.cursor.item/name
4. How the table is accessed
Content://com.android.contacts/raw_contacts
Content://com.android.contacts/data
The following is implemented in code
PrivateListView lv_contact; PrivatelistNewArraylist(); PrivateMyadapter Madapter; PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {//8, populating the data adapterMadapter =NewMyadapter (); Lv_contact.setadapter (Madapter); } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_contact_list); Initui (); InitData (); } classMyadapterextendsbaseadapter{@Override Public intGetCount () {returncontactlist.size (); } @Override Public hashmap<string, string>GetItem (inti) {returnContactlist.get (i); } @Override Public LongGetitemid (inti) {returni; } @Override PublicView GetView (intI, view view, ViewGroup ViewGroup) { View v = View.inflate (Getapplicationcontext (), R.layout.listview_contact_item, null ); TextView Tv_name=(TextView) V.findviewbyid (r.id.tv_name); TextView Tv_phone=(TextView) V.findviewbyid (R.id.tv_phone); Tv_name.settext (GetItem (i). Get ("Name")); Tv_phone.settext (GetItem (i). Get ("Phone")); returnv; } } /*** How to get Contact data*/ Private voidInitData () {//because reading a system contact, it can be a time-consuming operation that is placed in a child thread to process NewThread () { Public voidrun () {//1, Get Content Parser objectContentresolver Contentresolver =Getcontentresolver (); //2, do query system contact database table procedure (read contact permissions)cursor cursor =contentresolver.query (Uri.parse ("Content://com.android.contacts/raw_contacts"), Newstring[]{"contact_id"}, NULL,NULL,NULL); Contactlist.clear (); //3, loop the cursor until no data is present while(Cursor.movetonext ()) {String ID= cursor.getstring (0); //4, according to the user Uniqueness ID value, query data table and MimeType table generated view, get data and mimetype fieldCursor Indexcursor =contentresolver.query (Uri.parse ("Content://com.android.contacts/data"), Newstring[]{"Data1", "MimeType"}, "raw_contact_id =?",NewString[]{id},NULL); //5, loop to get the phone number and name of each contact, data typehashmap<string, string> HashMap =NewHashmap<string, string>(); while(Indexcursor.movetonext ()) {String data= indexcursor.getstring (0); String type= indexcursor.getstring (1); //6, differentiate types to fill hashmap data if(Type.equals ("Vnd.android.cursor.item/phone_v2")) { //data non-null judgment if(!textutils.isempty (data)) {Hashmap.put ("Phone", data); } }Else if(Type.equals ("Vnd.android.cursor.item/name")) { if(!textutils.isempty (data)) {Hashmap.put ("Name", data); }}} Indexcursor.close (); Contactlist.add (HASHMAP); } Cursor.close (); //7, message mechanism, sends an empty message informing the main thread that it can go to a collection of data that the child thread has already populated mhandler.sendemptymessage (0); }}.start (); }
The effect of the implementation is as follows:
Contact information Echo
Next implementation Click the contact entry, implement Echo, for example, double-click the first entry, the number is automatically added
The code is as follows:
Private voidInitui () {lv_contact=(ListView) Findviewbyid (r.id.lv_contact); Lv_contact.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> adapterview, view view,intILongl) {//1, gets the index of the entry in the point point to the object in the collection if(Madapter! =NULL) {HashMap<string, string> HashMap =Madapter.getitem (i); //2, get the current entry point to the collection corresponding phone numberString phone = hashmap.get ("Phone"); //3, this phone number needs to be used for a third navigation interface//4, at the end of this interface back to the previous navigation interface, you need to return the data to the pastIntent Intent =NewIntent (); Intent.putextra ("Phone", phone); Setresult (0, intent); Finish (); } } }); }
Then add the following code in the Onactivityresult
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { if(data! = NULL ) { //1, return to the current interface, the method of accepting the resultString phone = Data.getstringextra ("Phone"); //2. Filter special characters (medium dash to empty string)Phone = Phone.replace ("-", ""). Replace ("", ""). Trim (); Et_phone_number.settext (phone); //3, Store contacts to SP sputil.putstring (Getapplicationcontext (), Constantvalue.contact_phone, PHONE); } Super. Onactivityresult (Requestcode, ResultCode, data); }
When the number is filled, go to the next page, return again, find the number is missing, then use SP storage and read from it
Private voidInitui () {//Display the input box for the phone numberEt_phone_number =(EditText) Findviewbyid (R.id.et_phone_number); //Get Contact phone number echo process String contact_phone = sputil.getstring (This, Constantvalue.contact_phone, ""); Et_phone_number.settext (Contact_phone); Bt_select_number=(Button) Findviewbyid (R.id.bt_select_number); //Click the Select Contacts dialog box.Bt_select_number.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (Getapplicationcontext (), contactlistactivity.class); Startactivityforresult (Intent,0); } }); }
Android phone defender--Get contact information and display with Echo