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
// Open the address book with implicit intent
Intent intent =newIntent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,1001);
// Obtain the returned result after selection
@Override
protectedvoid onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode ==1001){
if(resultCode == RESULT_OK){
ContentResolver resolver = getContentResolver();
Uri uri = data.getData();
//uri = content://com.android.contacts/contacts/lookup/0r5-D9ADC7B9BBC9C7B9DBBDADC9/5
Cursor cursor = resolver.query(uri,null,null,null,
null);
if(cursor.moveToFirst()){
for(int i =0; i < cursor.getColumnCount(); i++){
Log.i("-->", cursor.getColumnNames()[i]+":"+ cursor.getString(i));
}
sort_key:wang hongxiao
photo_uri:null
send_to_voicemail:0
contact_status:null
contact_status_label:null
pinned:0
display_name:wang hongxiao
phonebook_label_alt:H
phonebook_bucket:23
contact_status_res_package:null
in_default_directory:1
photo_id:null
custom_ringtone:null
_id:5
times_contacted:0
phonebook_label:W
display_name_alt:hongxiao, wang
lookup:0r5-D9ADC7B9BBC9C7B9DBBDADC9
phonetic_name:null
last_time_contacted:0
contact_last_updated_timestamp:1477791122100
has_phone_number:1
in_visible_group:1
display_name_source:40
photo_file_id:null
is_user_profile:0
contact_status_ts:null
sort_key_alt:hongxiao, wang
phonebook_bucket_alt:8
contact_presence:null
starred:0
photo_thumb_uri:null
contact_status_icon:null
contact_chat_capability:null
phonetic_name_style:0
name_raw_contact_id:5
Through analysis, we can get the three fields we care about.
From Weizhi note (Wiz)