Android opens the system Contact Interface

Source: Internet
Author: User

In the development of android applications, it is often necessary to call the contact interface that comes with the system. After selecting a person, you can obtain the corresponding name and number and return it.

There are also some differences in implementation methods before and after android2.0: The main difference is that the contact API of Version 2.0 and later has changed from the original Contacts to ContactsContract.

(1) how to retrieve the contact list before 2.0:
Method 1
Intent intent = new Intent ();
Intent. setAction (Intent. ACTION_PICK );
Intent. setData (Contacts. People. CONTENT_URI );
StartActivityForResult (intent,
 
PICK_CONTACT
 
);
Method 2
Intent intent = new Intent (Intent. ACTION_PICK );
Intent. setType ("Contacts. People. CONTENT_TYPE"); // vnd. android. cursor. dir/person
StartActivityForResult (intent,
 
PICK_CONTACT
 
);
OnActivityResult
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
Switch (requestCode ){
Case
 
PICK_CONTACT
 
:
If (data = null ){
Return;
}
Uri uri = data. getData ();
Cursor cursor = getContentResolver (). query (uri, null );
Cursor. moveToFirst ();
String number = cursor. getString (cursor. getColumnIndexOrThrow (Phones. NUMBER ));
Log. d (TAG, "number" + number );
MContactText. setText (number );
MContactText. setSelection (number. length ());
Break;

Default:
Break;
}
}
 
(2) how to retrieve the contact list after 2.0:
Method 1

Intent intent = new Intent (Intent. ACTION_PICK, ContactsContract. Contacts. CONTENT_URI );
StartActivityForResult (intent, PICK_CONTACT );
Method 2
Intent intent = new Intent (Intent. ACTION_PICK );
Intent. setType (ContactsContract. Contacts. CONTENT_TYPE); // vnd. android. cursor. dir/contact
StartActivityForResult (intent, PICK_CONTACT );
OnActivityResult
@ Override
Public void onActivityResult (int reqCode, int resultCode, Intent data ){
Super. onActivityResult (reqCode, resultCode, data );
 
Switch (reqCode ){
Case (PICK_CONTACT ):
If (resultCode = Activity. RESULT_ OK ){
Uri contactData = data. getData ();
Cursor c = managedQuery (contactData, null );
If (c. moveToFirst ()){
String name = c. getString (c. getColumnIndex (ContactsContract. Contacts. DISPLAY_NAME ));
String hasPhone = cursor. getString (cursor. getColumnIndex (ContactsContract. Contacts. HAS_PHONE_NUMBER ));
String phoneNumber = null;
If (hasPhone. inclusignorecase ("1 ")){
HasPhone = "true ";
} Else {
HasPhone = "false ";
}
If (Boolean. parseBoolean (hasPhone )){
Cursor phones = getContentResolver (). query (ContactsContract. CommonDataKinds. Phone. CONTENT_URI, null, ContactsContract. example. Phone. CONTACT_ID + "=" + contactId, null, null );
While (phones. moveToNext ()){
PhoneNumber = phones. getString (phones. getColumnIndex (ContactsContract. CommonDataKinds. Phone. NUMBER ));
}
Phones. close ();
}
}
}
Break;
}
}


From a lonely sharer
 

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.