2020-03-29

1.ContentProvider

①创建一个数据库及一张表
②自定义ContentProvider继承自ContentProvider,重写方法

2.ContentResolver

①获取ContentResolver
②获取uri:Uri.parse("content://"+authorities+/+path)
Uri uri = Uri.parse("content://com.anfly.contentproviderr.ClContentProvider/cl");

3.通过内容解析者读取通讯录信息

ContactsContract.CommonDataKinds.Phone.CONTENT_URI 电话号码的Uri  
/**
 * 获取通讯录的数据
*/
private void initContacts() {
// 内容解析者
   Cursor query = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
   StringBuilder stringBuilder = new StringBuilder();
   while (query.moveToNext()) {
   String name = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
   String phoneNumber = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
         stringBuilder.append(name + "--" + phoneNumber);
   }
        Log.d(TAG, "initContacts: ");
        mResult.setText(stringBuilder.toString());
}

4.通过内容解析者读取短息信息

public void initSms(){
    // 短信的URI 地址要牢记
    Cursor query = getContentResolver().query(Telephony.Sms.CONTENT_URI, null, null, null, null);
    while (query.moveToNext()){
                    // 短信的关键字段  地址,内容,日期
        String address = query.getString(query.getColumnIndex(Telephony.Sms.ADDRESS));
        String body = query.getString(query.getColumnIndex(Telephony.Sms.BODY));
        String data = query.getString(query.getColumnIndex(Telephony.Sms.DATE));
        Log.d(TAG, "initSms: ="+address+"--boy="+body+"==="+data);
    }
}

5.通过内容解析者读取图片

private void initIMage() {
    //  media.Image  audio  video
    Cursor query = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    StringBuilder stringBuilder = new StringBuilder();
    while (query.moveToNext()){
        String name = query.getString(query.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
        String data = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
        stringBuilder.append(name+"=="+data+"\n");
    }
    mResult.setText(stringBuilder.toString());
}

你可能感兴趣的:(2020-03-29)