1.概念讲解
(1)ContentProvider的概述:
当我们想允许自己的应用数据允许别的应用进行读取操作,我们可以让我们的App实现ContentProvider类,同时注册一个Uri,然后其他应用只要使用ContentResolver根据Uri就可以操作我们的App中的数据了!而数据库不一定是数据库,也可能是文件,xml,或者其他,但是SharePreference使用基于数据库模型的简单表格来提供其中的数据!
(2)ContentProvider的执行原理
(3)URI简介:
专业名词叫做:通用资源标识符,我们也可以类比为网页的域名,就是定位资源所在路径。例如:
Uri uri = Uri.parse(“content://com.andriod.contacts/raw_contacts”);
content:协议头,这是规定的,就像http,ftp等一样,而ContentProvider规定的是content开头的,接着是provider所在的全限定类名。
com.andriod.contacts代表资源部分,如果想访问com.andriod.contacts所有资源,后面就不用写了。
raw_contacts访问的是com.andriod.contacts资源中id为 raw_contacts的记录。
2.示例(访问手机通讯录)
(1)打开android studio新建一个项目,创建一个ContentProviderActivity.java文件和layout.xml文件,如图:
layout.xml文件
(2)写入两个按钮
ContentProviderActivity.java文件
(3)获取读、写的动态权限
注意:需要在AndroidManifest.xml文件中添加读写的权限获取代码
下面为java代码块
int permissionCheck = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS);
if(permissionCheck != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},0);
}
public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults){
switch (requestCode){
case 0:
if ((grantResults.length > 0)&&(grantResults[0] == PackageManager.PERMISSION_GRANTED)){
Toast.makeText(ContentProviderActivity.this,"联系人授权成功",Toast.LENGTH_SHORT).show();
int permissionCheck = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_CONTACTS);
if(permissionCheck != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_CONTACTS},1);
}
}
break;
case 1:
if ((grantResults.length > 0)&&(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(ContentProviderActivity.this, "写入联系人权限授权成功", Toast.LENGTH_SHORT).show();
}
default:
break;
}
}
(4)获取联系人信息
private void getContacts(){
//查询raw_contacts表获得联系人的id
ContentResolver resolver = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//查询联系人数据
Cursor cursor = resolver.query(uri,null,null,null,null);
while (cursor.moveToNext()){
//获取联系人姓名,手机号码
String cName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String cNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("","姓名:"+cName);
Log.e("","号码:"+cNum);
Log.e("","=====================");
}
cursor.close();
}
private void addContact(){
try {
//使用事务添加联系人
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri dataUri = Uri.parse("content://com.android.contacts/data");
ContentResolver resolver = getContentResolver();
ArrayList operations = new ArrayList<>();
//为了便于Android中进行批量数据库操作时效率更高,Android中推荐使用ContentProviderOperation
ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
.withValue("account_name",null)
.build();
operations.add(op1);
//依次是姓名,号码,邮编
ContentProviderOperation op2 =ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id",0)
.withValue("mimetype","vnd.android.cursor.item/name")
.withValue("data2","Harden")
.build();
operations.add(op2);
ContentProviderOperation op3 =ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id",0)
.withValue("mimetype","vnd.android.cursor.item/phone_v2")
.withValue("data1","15263348325")
.build();
operations.add(op3);
ContentProviderOperation op4 =ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id",0)
.withValue("mimetype","vnd.android.cursor.item/email_v2")
.withValue("data1","[email protected]")
.build();
operations.add(op4);
//将上述内容添加到手机联系人中
resolver.applyBatch("com.android.contacts",operations);
Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Log.e("",e.getMessage());
}
}
}
作者:陈绍波
原文链接:
https://blog.csdn.net/qq_44766524/article/details/112001082?utm_source=app