ContentProvider: The Central Archive, as well as an example of getting a contact's phone number. contentprovider

Source: Internet
Author: User

ContentProvider: The Central Archive, as well as an example of getting a contact's phone number. contentprovider
The Android official document describes five Data Storage Methods: sqlite, SharedPreferences, network storage, external storage, and file storage. However, these data cannot be shared, then we will introduce the main character of today: ContentProvider

  • ContentResolver can operate data in ContentProvider. You can use the getContentResolver () method provided by Activity. It has four methods with the same signature: insert, update, delete, query
  • Get contact information
    1. // Open the address book with implicit intent
    2. Intent intent =newIntent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
    3. startActivityForResult(intent,1001);
    1. // Obtain the returned result after selection
    2. @Override
    3. protectedvoid onActivityResult(int requestCode,int resultCode,Intent data){
    4. super.onActivityResult(requestCode, resultCode, data);
    5. if(requestCode ==1001){
    6. if(resultCode == RESULT_OK){
    7. ContentResolver resolver = getContentResolver();
    8. Uri uri = data.getData();
    9. //uri = content://com.android.contacts/contacts/lookup/0r5-D9ADC7B9BBC9C7B9DBBDADC9/5
    10. Cursor cursor = resolver.query(uri,null,null,null,
    11. null);
    12. if(cursor.moveToFirst()){
    13. for(int i =0; i < cursor.getColumnCount(); i++){
    14. Log.i("-->", cursor.getColumnNames()[i]+":"+ cursor.getString(i));
    15. }
     
    1. sort_key:wang hongxiao
    2. photo_uri:null
    3. send_to_voicemail:0
    4. contact_status:null
    5. contact_status_label:null
    6. pinned:0
    7. display_name:wang hongxiao
    8. phonebook_label_alt:H
    9. phonebook_bucket:23
    10. contact_status_res_package:null
    11. in_default_directory:1
    12. photo_id:null
    13. custom_ringtone:null
    14. _id:5
    15. times_contacted:0
    16. phonebook_label:W
    17. display_name_alt:hongxiao, wang
    18. lookup:0r5-D9ADC7B9BBC9C7B9DBBDADC9
    19. phonetic_name:null
    20. last_time_contacted:0
    21. contact_last_updated_timestamp:1477791122100
    22. has_phone_number:1
    23. in_visible_group:1
    24. display_name_source:40
    25. photo_file_id:null
    26. is_user_profile:0
    27. contact_status_ts:null
    28. sort_key_alt:hongxiao, wang
    29. phonebook_bucket_alt:8
    30. contact_presence:null
    31. starred:0
    32. photo_thumb_uri:null
    33. contact_status_icon:null
    34. contact_chat_capability:null
    35. phonetic_name_style:0
    36. name_raw_contact_id:5
    Through analysis, we can get the three fields we care about.
  •  



    From Weizhi note (Wiz)



    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.