Get contact information for Android Development

Source: Internet
Author: User

For mobile phones, there are two types of contacts: SIM card contacts and built-in mobile phone contacts. Generally, SIM card contacts do not contain more information (such as mailing addresses and portraits), but mobile phone built-in contacts can obtain more information. Basically, this information is bound to a mobile phone account, so that mobile phone users can retrieve lost data. The following describes how to obtain contact information:

Java File Code (Part ):

/** Obtain the Phon table field of the database **/

Private staticfinal String [] PHONES_PROJECTION = new String [] {

Phone. DISPLAY_NAME, Phone. NUMBER, Photo. PHOTO_ID, Phone. CONTACT_ID };


/** Display contact name **/

Privatestatic finalint PHONES_DISPLAY_NAME_INDEX = 0;


/** Phone number **/

Private staticfinal int PHONES_NUMBER_INDEX = 1;


/** Avatar ID **/

Private staticfinal int PHONES_PHOTO_ID_INDEX = 2;


/** Contact ID **/

Privatestatic finalint PHONES_CONTACT_ID_INDEX = 3;


/** Contact name **/

Private ArrayList MContactsName = new ArrayList ();


Public ArrayList GetmContactsName (){

ReturnmContactsName;

}


Public void setmContactsName (ArrayList MContactsName ){

This. mContactsName = mContactsName;

}


Public ArrayList GetmContactsNumber (){

ReturnmContactsNumber;

}


Public void setmContactsNumber (ArrayList MContactsNumber ){

This. mContactsNumber = mContactsNumber;

}


/** Contact profile picture **/

Private ArrayList MContactsNumber = new ArrayList ();


/** Contact profile picture **/

Private ArrayList MContactsPhonto = new ArrayList ();


/** Get the mobile phone Address Book Contact Information **/

Public void getPhoneContacts (){


ContentResolver resolver = context. getContentResolver ();


// Obtain the mobile phone contact

Cursor phoneCursor = resolver. query (Phone. CONTENT_URI,

PHONES_PROJECTION, null );


If (phoneCursor! = Null ){

While (phoneCursor. moveToNext ()){


// Obtain the mobile phone number

String phoneNumber = phoneCursor. getString (PHONES_NUMBER_INDEX );

// When the mobile phone number is empty or empty, the current loop is skipped.

If (TextUtils. isEmpty (phoneNumber ))

Continue;


// Obtain the contact name

String contactName = phoneCursor

. GetString (PHONES_DISPLAY_NAME_INDEX );


/// Obtain the contact ID

// Longcontactid =

// PhoneCursor. getLong (PHONES_CONTACT_ID_INDEX );

//

/// Obtain the Contact Profile ID

// Longphotoid = phoneCursor. getLong (PHONES_PHOTO_ID_INDEX );

//

//// Get the Contact Profile Bitamp

// Bitmap contactPhoto = null;


// If the photoid is greater than 0, it indicates that the contact has an Avatar. If no Avatar is set for this person, a default one is provided.

// If (photoid> 0 ){

// Uri uri = ContentUris. withAppendedId (

// ContactsContract. Contacts. CONTENT_URI, contactid );

// InputStream input = ContactsContract. Contacts

//. OpenContactPhotoInputStream (resolver, uri );

// ContactPhoto = BitmapFactory. decodeStream (input );

//} Else {

// ContactPhoto = BitmapFactory. decodeResource (

// M. getResources (), R. drawable. contact_photo );

//}


MContactsName. add (contactName );

MContactsNumber. add (phoneNumber );

}


PhoneCursor. close ();

}

}


/** Get the contact person information for the SIM card on your mobile phone **/

Public void getSIMContacts (){


ContentResolver resolver = context. getContentResolver ();

// Obtain the Sims Card Contact

Uri uri = Uri. parse ("content: // icc/adn ");

Cursor phoneCursor = resolver. query (uri, PHONES_PROJECTION, null, null,

Null );


If (phoneCursor! = Null ){

While (phoneCursor. moveToNext ()){


// Obtain the mobile phone number

String phoneNumber = phoneCursor. getString (PHONES_NUMBER_INDEX );

// When the mobile phone number is empty or empty, the current loop is skipped.

If (TextUtils. isEmpty (phoneNumber ))

Continue;

// Obtain the contact name

String contactName = phoneCursor

. GetString (PHONES_DISPLAY_NAME_INDEX );


// No contact profile in SIM card


MContactsName. add (contactName );

MContactsNumber. add (phoneNumber );

}


PhoneCursor. close ();

}

}

Permissions that must be applied for in the MainFest. xml file:

Success!

If you have reprinted the results of your hard work, please indicate the source. Thank you!

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.