Andriod reads the address book, andriod address book

Source: Internet
Author: User

Andriod reads the address book, andriod address book

Package com. example. yanlei. wifi;

Import android. content. ContentResolver;
Import android. database. Cursor;
Import android. OS. Bundle;
Import android. provider. ContactsContract;
Import android. provider. ContactsContract. PhoneLookup;
Import android. support. v7.app. AppCompatActivity;
Import android. util. Log;
Import android. widget. Button;
Import android. widget. ListView;
Import android. widget. TextView;

Public class MainActivity extends AppCompatActivity {

Private ListView lvContacts = null;
Private Cursor cursor = null;
Private Button btnRead = null;


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TextView TV = new TextView (this );
String str = "";
// Obtain the contentresolver object
ContentResolver cr = getContentResolver ();
// Obtain the cursor of the Start item in the phone book. You must first moveToNext ()
Cursor cursor = cr. query (ContactsContract. Contacts. CONTENT_URI, null,
Null, null, null );
While (cursor. moveToNext ())
{
// Retrieve the contact name index
Int nameIndex = cursor. getColumnIndex (PhoneLookup. DISPLAY_NAME );
String contact = cursor. getString (nameIndex );
Str + = (contact + ":" + "\ n ");
// Obtain the contact's ID index value
String contactId = cursor. getString (cursor
. GetColumnIndex (ContactsContract. Contacts. _ ID ));
Cursor phone = cr. query (
ContactsContract. CommonDataKinds. Phone. CONTENT_URI, null,
ContactsContract. CommonDataKinds. Phone. CONTACT_ID + "="
+ ContactId, null, null );

// A person may have several numbers
While (phone. moveToNext ())
{
String strPhoneNumber = phone
. GetString (phone
. GetColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER ));
Str + = (strPhoneNumber + "\ n ");
Log. I ("TAG", "phoneNumber:" + strPhoneNumber + "contact" + contact );
}
Phone. close ();
}
Cursor. close ();
TV. setText (str );
SetContentView (TV );

}



}

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.