ImportAndroid.content.ContentResolver;ImportAndroid.content.ContentUris;ImportAndroid.content.Context;ImportAndroid.database.Cursor;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;Importandroid.provider.ContactsContract;Importandroid.text.TextUtils;ImportCom.app.common.util.Log;ImportCOM.GZSHAPP.GZSH.R;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;/*** Address Book control class 1, Get phone contacts contact information 2, get phone SIM card contact information*/ Public classContactscontroller {Private FinalString TAG = "Contact"; /*** Get the Library phone table field*/ Private Static Finalstring[] Phones_projection =Newstring[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, Contactscontract.commondatakinds . Phone.number, ContactsContract.CommonDataKinds.Photo.PHOTO_ID, ContactsContract.CommonDataKinds.Phon E.CONTACT_ID}; /*** Display name of the contact person **/ Private Static Final intPhones_display_name_index = 0; /*** Phone number **/ Private Static Final intPhones_number_index = 1; /*** Avatar id**/ Private Static Final intPhones_photo_id_index = 2; /*** Contact person's id**/ Private Static Final intPhones_contact_id_index = 3; PrivateContext Mcontext; PublicContactscontroller (Context mcontext) { This. Mcontext =Mcontext; }/*** Get all the phone contacts*/ PublicList<contactsmodel>getcontact () {//Contact ListList<contactsmodel> contactslist =NewArraylist<contactsmodel>(); //get all the contactsCursor cur = mcontext.getcontentresolver (). Query (ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL); //Looping through if(Cur.movetofirst ()) {intIdcolumn =Cur.getcolumnindex (contactscontract.contacts._id); intDisplaynamecolumn =Cur.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME); Do{Contactsmodel model=NewContactsmodel (); List<String> phonenumbers =NewArraylist<string>(); //get the ID number of the contact personString contactId =cur.getstring (Idcolumn); Model.setcontactid (long.valueof (contactId)); //get name of contact personString ContactName =cur.getstring (Displaynamecolumn); Model.setcontactname (ContactName); //See how many phone numbers the contact has. If not, the return value is 0 . intPhonecount =Cur.getint (Cur.getcolumnindex (ContactsContract.Contacts.HAS_PHONE_NUMBER)); if(Phonecount > 0) { //get the phone number of the contact personCursor phones =mcontext.getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId,NULL,NULL); if(Phones.movetofirst ()) { Do { //traverse all the phone numbersString PhoneNumber =phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Phonenumbers.add (Formatphonenumber (PhoneNumber)); Model.setphonenumbers (phonenumbers);//System.out.println (phonenumber);} while(Phones.movetonext ()); }} contactslist.add (model); } while(Cur.movetonext ()); } if(NULL!=cur) {Cur.close (); } //Print//print (contactslist);LOG.E (TAG, "phone directory Total" + contactslist.size () + "record"); returncontactslist; } Private voidPrint (list<contactsmodel>contactslist) { for(inti = 0; I < contactslist.size (); i++) {String msg= Contactslist.get (i). Getcontactname () + ":"; List<String> phones =Contactslist.get (i). Getphonenumbers (); String Phone= ""; if(NULL! = Phones && phones.size () > 0) { for(intj = 0; J < Phones.size (); J + +) {Phone+ = Phones.get (j) + ","; }} msg+=phone; SYSTEM.OUT.PRINTLN (msg); } } /*** Get mobile SIM card contact information for everyone*/ PublicList<contactsmodel>getsimcontacts () {//Contact ListList<contactsmodel> contactslist =NewArraylist<contactsmodel>(); Contentresolver Resolver=Mcontext.getcontentresolver (); //get Sims Card Contact personUri uri = uri.parse ("Content://icc/adn"); Cursor Phonecursor= Resolver.query (URI, Phones_projection,NULL,NULL, NULL); if(Phonecursor! =NULL) { while(Phonecursor.movetonext ()) {//get a cell phone numberString PhoneNumber =phonecursor.getstring (Phones_number_index); //when the cell phone number is empty or an empty field skips the current loop if(Textutils.isempty (phonenumber))Continue; //Get Contact NameString ContactName =phonecursor. getString (Phones_display_name_index); //no contact with the head image in the SIM card.Contactsmodel model =NewContactsmodel (); List<String> phonenumbers =NewArraylist<string>(); Phonenumbers.add (PhoneNumber); Model.setcontactname (ContactName); Model.setphonenumbers (phonenumbers); Contactslist.add (model); } phonecursor.close (); } returncontactslist; } /*** Formatted mobile phone number * *@paramPhoneNumber *@return */ Privatestring Formatphonenumber (String phonenumber) {//Go to SpacePhoneNumber = Phonenumber.replace ("", "" "). Trim (); //Remove the +86 opening if(Phonenumber.startswith ("+86") ) {PhoneNumber= Phonenumber.substring (3); } //Removal- if(Phonenumber.contains ("-") ) {PhoneNumber= Phonenumber.replace ("-", "" "). Trim (); } //Remove Area code if(Phonenumber.startswith ("0") &&(Phonenumber.length ()= = 11 | | Phonenumber.length () = = 12) ) {PhoneNumber= Phonenumber.substring (4); } returnPhoneNumber; } /*** Remove the phone number duplicate * *@paramContactslist *@return */ PrivateList<contactsmodel> Megercontactslist (list<contactsmodel>contactslist) { for(inti = 0; I < contactslist.size (); i++) {Contactsmodel model=Contactslist.get (i); for(intj = contactslist.size ()-1; J > 0; j--) {Contactsmodel Tempmodel=Contactslist.get (j); if(NULL! = Tempmodel &&NULL!=model) {List<String> phones =model.getphonenumbers (); List<String> Tempphones =tempmodel.getphonenumbers (); if(NULL! = Phones && phones.size () > 0 &&NULL! = Tempphones && tempphones.size () > 0) if(Phones.get (0). Equals (Tempphones.get (0)) {contactslist.remove (j); } } } } returncontactslist; }}
Read Local Address Book