ContentProvider ——> ContentResolver
内容提供者 URI 内容访问者
URI:统一资源标识符 //URL(是URI的一种):统一资源定位符
注册ContentProvider:
name:全路径
authorities:认证,Uri的一部分,name的小写 // uri = Uri.parse("content://" + AUTHORITY);
exported:能否让别的应用程序访问当前程序的数据
1.ContentResolver:
a.获取通话记录 权限:READ_CALL_LOG
Uri uri = CallLog.Calls.CONTENT_URI; // Uri.parse("content://call_log/calls")
private ContentResolver resolver;
resolver = getContentResolver();
String[] projection = new String{
CallLog.Calls._ID, //"_id"
CallLog.Calls.CACHED_NAME, //"name"
CallLog.Calls.NUMBER, //"number"
CallLog.Calls.TYPE, //"type"
CallLog.Calls.DATE //"date"
};
Cursor cursor = resolver.query(uri,projection,null,null,null);
b.获取短信记录 权限:READ_SMS
Uri uri = Sms.CONTENT_URI; // Uri.parse("content://sms")
String[] projection = new String[]{
Sms.Draft._ID, //"_id"
Sms.Draft.ADDRESS, //"address"
Sms.Draft.BODY, //"body"
Sms.Draft.TYPE //"type"
};
SimpleCursorAdapter()中from为projection
c.联系人 权限:READ_CONTACTS、WRITE_CONTACTS
两张表 View_raw_contacts、View_data(在data/data/com.android.contacts/database/contact2.db数据库中)
Uri rawUri = ContactsContract.RAWContacts.CONTENT_URI; // Uri.parse("content://com.android.contacts/raw_contacts")
Uri dataUri = ContactsContract.Data.CONTENT_URI; // Uri.parse("content://com.android.contacts/data")
Cursor rawCursor = resolver.query(rawUri, new String[]{"_id","contact_id",null,null,null});
while(rawCursor.moveToNext()){
String contact_id = rawCursor.getString(rawCursor.getColumnIndex("contact_id"));
Cursor dataCursor = resolver.query(dataUri,
new String[]{""_id,"data1","mimtype"},
"raw_contact_id=?",
new String[]{contact_id},
null);
while(dataCursor.moveToNext()){
String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));
String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
Log.i("info","联系人ID:" + contact_id + ",电话:" + data1); } else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) { Log.i("info",""联系人ID:" + contact_id + ",email:" + data1); } else if ("vnd.android.cursor.item/name".equals(mimetype)) { Log.i("info",""联系人ID:" + contact_id + ",联系人姓名:" + data1); }}
}
不能用SimpleCursorAdapter
2.Android四大组件之ContentProvider