Android obtains the contact name, phone number, and other information in the system in its own activity.

Source: Internet
Author: User

 
  1. Public class contracttest extends activity {
  2. Private Static final string tag = "u0fly contracttest ==> ";
  3. Private Static final int pick_contact = 3;
  4. @ Override
  5. Public void oncreate (bundle savedinstancestate ){
  6. Super. oncreate (savedinstancestate );
  7. Setcontentview (R. layout. Main );
  8. Log. D (TAG, "oncreate ");
  9. Textview TV = (textview) findviewbyid (R. Id. textview );
  10. Button BTN = (button) findviewbyid (R. Id. BTN );
  11. BTN. setonclicklistener (New View. onclicklistener (){
  12. @ Override
  13. Public void onclick (view v ){
  14. // Todo auto-generated method stub
  15. Intent intent = new intent (intent. action_pick, contactscontract. Contacts. content_uri );
  16. Startactivityforresult (intent, pick_contact );
  17. }
  18. });
  19. }
  20. @ Override
  21. Public void onactivityresult (INT reqcode, int resultcode, intent data ){
  22. Super. onactivityresult (reqcode, resultcode, data );
  23. Log. D (TAG, "onactivityresult ");
  24. Switch (reqcode ){
  25. Case (pick_contact ):
  26. Log. D (TAG, "onactivityresult pick_contact ");
  27. If (resultcode = activity. result_ OK ){
  28. Uri contactdata = data. getdata ();
  29. Contentresolver c = getcontentresolver ();
  30. Cursor cursor = C. Query (contactdata, null );
  31. If (cursor. movetofirst ()){
  32. String [] phones_projection = new string [] {"_ id", "display_name", "data1", "data3 "};
  33. String contactid = cursor. getstring (cursor. getcolumnindex (phonelookup. _ id ));
  34. Cursor phone = C. Query (
  35. Contactscontract. commondatakinds. Phone. content_uri,
  36. Phones_projection,
  37. Contactscontract. commondatakinds. Phone. contact_id
  38. + "=" + Contactid, null, null );
  39. // Name type ..
  40. While (phone. movetonext ()){
  41. Int I = phone. getint (0 );
  42. String STR = phone. getstring (1 );
  43. STR + = "";
  44. STR + = phone. getstring (2 );
  45. STR + = "";
  46. STR + = phone. getstring (3 );
  47. }
  48. // Other data is available for the contact. I have decided
  49. // To only get the name of the contact.
  50. String name = cursor. getstring (cursor. getcolumnindexorthrow (contactscontract. Contacts. display_name ));
  51. Log. D (TAG, "onactivityresult pick_contact result_ OK 444 ");
  52. // Toast. maketext (getapplicationcontext (), name, Toast. length_short). Show ();
  53. Textview TV = (textview) findviewbyid (R. Id. textview );
  54. TV. settext (name );
  55. }
  56. }
  57. }
  58. }
  59. }

 

Add permission:

<Uses-Permission Android: Name = "android. Permission. read_contacts"/>

 

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.