Android Jump to Address book to get the user name and mobile phone number of the implementation of ideas _android

Source: Internet
Author: User

The effect diagram looks like this:

First of all, let's say to realize Android jump to the Address Book realization idea:

1. Click to jump to the Address Book interface

2. Get Address Book name and cell phone number

3. Callback display name and cell phone number

1 first jump to the Address Book interface

Uri uri = uri.parse ("Content://contacts/people");
Intent Intent = new Intent (Intent.action_pick, URI);
Startactivityforresult (Intent, 0);

By setting the Address Book URL jump, you can see that we use the callback function to implement

2. Callback function


 /* The callback function of the Jump contact list * * *
 /@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  switch (requestcode) {case
   0:
    if (data==null)
    {return
     ;
    }
    Processes the returned data, gets the selected contact information
    Uri uri=data.getdata ();
    String[] Contacts=getphonecontacts (URI);
    Et_name.settext (Contacts[0]);
    Et_tele.settext (Contacts[1]);
    break;
  Super.onactivityresult (Requestcode, ResultCode, data);
 }

Where the Getphonecontacts (URI) method, because the phone's contact and cell phone number are no longer in the same database, so we need to do separate processing

 private string[] Getphonecontacts (Uri uri) {string[] contact=new string[2];
  Get Contentresolver object Contentresolver cr = Getcontentresolver ();
  Gets the cursor Cursor cursor=cr.query (uri,null,null,null,null) in the beginning of the phone book;
   if (cursor!=null) {Cursor.movetofirst ();
   Get Contact name int Namefieldcolumnindex=cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME);
   Contact[0]=cursor.getstring (Namefieldcolumnindex);
   Get phone number 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);
    if (phone!= null) {Phone.movetofirst ();
   CONTACT[1] = phone.getstring (Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));
   } phone.close ();
  Cursor.close ();
  else {return null;
 return to contact; }

3. Additional Privileges

<!--get Address Book permissions-->
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
< Uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

The above is a small set to introduce Android jump to the address book to get the user name and mobile phone number of the realization of ideas, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.