How to query contact information in Android

Source: Internet
Author: User

In the Android operating system, programmers can make some modifications based on their own needs to easily complete various functions. This open-source system is undoubtedly a development opportunity for mobile phone manufacturers. Here we will introduce a method for Android to query contact information to help you solve some problems.

The following Android function is used to query the contact name, phone number, and email address,

 
 
  1. String columns [] = new String [] {People. _ ID, People. NAME,
    People. NUMBER, People. PRIMARY_EMAIL_ID,
  2. People. PRIMARY_ORGANIZATION_ID, People.
    PRIMARY_PHONE_ID, People. DISPLAY_NAME,
  3. People. IM_ACCOUNT, People. IM_HANDLE, People. PHONETIC_NAME, People. TYPE };
  4. Uri mContacts = People. CONTENT_URI;
  5. Cursor cur = managedQuery (mContacts, columns, // data field to be returned
  6. Null, // WHERE clause
  7. Null, // WHERE clause Parameters
  8. People. NAME // Order-by clause
  9. );
  10. If (cur. moveToFirst ()){
  11. Cursor newcur = null;
  12. Do {
  13. // Obtain the field value
  14. String name = cur. getString (cur. getColumnIndex (People. NAME ));
  15. String phoneNo = cur. getString (cur. getColumnIndex (People. NUMBER ));
  16. String peopleId = cur. getString (cur. getColumnIndex (People. _ ID ));
  17. String [] PROJECTION = new String [] {Contacts. ContactMethods. _ ID,
    Contacts. ContactMethods. KIND, Contacts. ContactMethods. DATA };
  18. Newcur = managedQuery (Contacts. ContactMethods. CONTENT_URI,
    PROJECTION, Contacts. ContactMethods. PERSON_ID + "= \'"
  19. + Cur. getLong (cur. getColumnIndex (People. _ ID) + "\ '", null, null );
  20. StartManagingCursor (newcur );
  21. String email = "";
  22. If (newcur. moveToFirst ()){
  23. Email = newcur. getString (newcur. getColumnIndex
    (Contacts. ContactMethods. DATA ));
  24. }
  25. Log.info ("name =" + name +"
    PhoneNo = "+ phoneNo +" email = "+ email );
  26. If (email! = Null &&! "". Equals (email )&&
    Email. trim (). length ()! = 0 ){
  27. // You can obtain the contact email here.
  28. }
  29. } While (cur. moveToNext ());
  30. If (newcur! = Null ){
  31. Newcur. close (); // close once used up
  32. }
  33. }
  34. If (cur! = Null)
  35. Cur. close (); // close once used up

By querying the email address above, you can query the contact's address information, company information, and so on. The specific Android Method for querying contact information is described here.

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.