Android get name and phone number of phone contact person

Source: Internet
Author: User

Android get name and phone number of phone contact person

The main use is to jump into the mobile phone contact intent and get mobile contact information content provider, directly on the code

Note: This paste is a reference to other people's posts added some of their own things to write, the original address is attached tomorrow:

/first we need to jump into the phone address Book Intent intent=NewIntent (Intent.action_pick, ContactsContract.Contacts.CONTENT_URI); Startactivityforresult (Intent,0);//after that, we need to rewrite Onactivityresult to parse the data returned from the contact@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    Super. Onactivityresult (Requestcode, ResultCode, data); Switch(requestcode) { Case0: {            if(ResultCode = =ACTIVITY.RESULT_OK) {Uri Contactdata=Data.getdata (); //This gives you a cursor with the contact data.Cursor C = managedquery (Contactdata,NULL,NULL,NULL,NULL); //move the cursor to the first position, and then we begin to parse the data in the cursor, where we extract a method;C.movetofirst (); //getcontactphone (cursor cursor)String phonenum[] = This. Getcontactphone (c); //finally get what you want to get things, installed in the phonenum[], want to do what the operation on the line;            }             Break; }    }}/*** Parameters: Cursor cursor; specify CURSOR * function: Remove contact name and phone from specified cursor and return * return value: string[] 0 Location is contact Name 1 location is contact phone*/Privatestring[] Getcontactphone (cursor cursor) {intPhonecolumn =Cursor.getcolumnindex (ContactsContract.Contacts.HAS_PHONE_NUMBER); intPhonenum =Cursor.getint (Phonecolumn); //the final string array to returnString phoneresult[] =NewString[2]; if(Phonenum > 0) {        //get the ID number of the contact person        intIdcolumn =Curso0r.getcolumnindex (contactscontract.contacts._id); String contactId=cursor.getstring (Idcolumn); //The cursor that gets the phone number of the contact person;Cursor phones =getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " +ContactId,NULL,NULL); if(Phones.movetofirst ()) {//traverse all the phone numbers             for(; !phones.isafterlast (); Phones.movetonext ()) {                intindex =Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER); intPhone_type =Phones.getint (Typeindex); //get the number of the selected contactString PhoneNumber =phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.TYPE)); //get the name of the selected contactString Phonename =phones.getstring (Phones.getcolumnindex (ContactsContract.PhoneLookup.DISPLAY_NAME)); //This one switch is not clear what can filter what, to return to the company tomorrow to try,                Switch(phone_type) { Case2: phoneresult[1] =PhoneNumber; phoneresult[0] =Phonename;  Break; }            }            //Finally, you want to close the cursor            if(!phones.isclosed ())            {Phones.close (); }        }    }    returnPhoneresult;}

Copy the above code, you can do it.

Android get name and phone number of phone contact person

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.