Android selects contacts + returns the corresponding mobile phone number

Source: Internet
Author: User

In my daily text message program, you can click the select contact button to display the contact selection page. After you click Select contact, the mobile phone number of the contact is added to a text box. For the introduction and interface of this program, see my first Android program.
You can directly use the built-in contacts of Android to select contacts. There are some examples on the Internet, but some codes are faulty. Some of them are normal in the simulator, cannot be used on the mobile phone, some are OK on the mobile phone, and some cannot be used on the simulator. The reason is that the contact URI uses hard encoding. You can use the ContactsContract. Contacts. CONTENT_URI constant to enhance portability.
The code for selecting a contact is as follows:
Java code
MChooseContactButton. setOnClickListener (new Button. OnClickListener (){
 
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent (Intent. ACTION_PICK, ContactsContract. Contacts. CONTENT_URI );
 
SMSSendActivity. this. startActivityForResult (intent, 1 );
}

});

The next step is to obtain the mobile phone number of the selected contact. First, you need to use the onActivityResult function to obtain the selected contact and then query the mobile phone number of the contact through the Content Provider. The code snippet is as follows:
Java code
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
Switch (requestCode)
{
 
Case (1 ):
{
 
If (resultCode = Activity. RESULT_ OK)
{
 
Uri contactData = data. getData ();
 
Cursor c = managedQuery (contactData, null );
 
C. moveToFirst ();
 
String phoneNum = this. getContactPhone (c );
MPhoneNum. setText (phoneNum );

}

Break;

}
 
}
 



}
 
 
// Obtain the contact phone number
Private String getContactPhone (Cursor cursor)
{
 
Int phoneColumn = cursor. getColumnIndex (ContactsContract. Contacts. HAS_PHONE_NUMBER );
Int phoneNum = cursor. getInt (phoneColumn );
String phoneResult = "";
// System. out. print (phoneNum );
If (phoneNum> 0)
{
// Obtain the contact ID
Int idColumn = cursor. getColumnIndex (ContactsContract. Contacts. _ ID );
String contactId = cursor. getString (idColumn );
// Obtain the cursor of the contact's phone number;
Cursor phones = getContentResolver (). query (
ContactsContract. CommonDataKinds. Phone. CONTENT_URI,
Null,
ContactsContract. CommonDataKinds. Phone. CONTACT_ID + "=" + contactId,
Null, null );
// Int phoneCount = phones. getCount ();
// AllPhoneNum = new ArrayList <String> (phoneCount );
If (phones. moveToFirst ())
{
// Traverse all phone numbers
For (;! Phones. isAfterLast (); phones. moveToNext ())
{
Int index = phones. getColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER );
Int typeindex = phones. getColumnIndex (ContactsContract. CommonDataKinds. Phone. TYPE );
Int phone_type = phones. getInt (typeindex );
String phoneNumber = phones. getString (index );
Switch (phone_type)
{
Case 2:
PhoneResult = phoneNumber;
Break;
}
// AllPhoneNum. add (phoneNumber );
}
If (! Phones. isClosed ())
{
Phones. close ();
}
}
}
Return phoneResult;
}

In this way, you can select a contact to obtain its mobile phone number. The code is directly copied from my program code and needs some modifications, but it is absolutely available.

The author is "a layman watching the fun"
 

Related Article

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.