简介
内容提供器主要用于在不同应用程序之间实现数据共享的功能,他可以选择只对那一部分数据进行共享,从而保证我们程序中的隐私数据不会有泄漏的风险。
例子:微信读取手机联系人信息
一.创建自己的内容提供器
一个app只有创建了自己的内容提供器,才能让别的程序去访问他共享的数据
1. 创建一个SQL数据库
要使用内容提供器肯定需要程序中有SQL数据库,这里只是简单的创建出来做个演示,可以用SQLiteDatabase创建,也可以用LitePal,不懂的点这里,我用LitePal创建了一个Demo库,litepal.xml和Demo1表如下
<?xml version="1.0" encoding="UTF-8" ?>
<litepal>
<dbname value="demo"/>
<version value="1"/>
<list>
<mapping class="com.example.revise_sqlitedatabase.Demo1"></mapping>
</list>
</litepal>
package com.example.revise_sqlitedatabase;
import org.litepal.crud.LitePalSupport;
public class Demo1 extends LitePalSupport {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2. 创建自己的内容提供器
右击包名->new->Other->ContentProvider
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6595b06fcf819d81f6fbbe351e22b342.png)
class name就是你的内容提供器的类名,URI Authorities就是URI的authority,一般写成 包名.provider
URI:给内容提供器中的数据建立了唯一的标识符,就是根据他知道要访问那个数据,由两部分组成:authority(上面创建时的URI Authorities),path(表名,访问那张表就写啥比如/table),为了看出这是URI还要加上头部协议,标准格式如下
content://com.example.app.provider/table
接下来吧URI解析成Uri对象,用Uri做参数就可以访问数据了
Uri uri = uri.parse(content://com.example.app.provider/table)
我的内容提供器如下,方法中的uri就是别的程序在调用时传入的,可根据他做出对应的操作
package com.example.revise_sqlitedatabase;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import org.litepal.LitePal;
public class MyContentProvider extends ContentProvider {
public static final int Demo1_DIR = 0;
public static final int Demo1_ITEM = 1;
public static final String AUTHORITY= "com.example.revise_sqlitedatabase.provider";
private static UriMatcher uriMatcher;
private Demo1 demo1;
SQLiteDatabase db;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"Demo1",Demo1_DIR);
uriMatcher.addURI(AUTHORITY,"Demo1/#",Demo1_ITEM);
}
public MyContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int deletedRows = 0;
switch (uriMatcher.match(uri)){
case Demo1_DIR:
deletedRows = db.delete("Demo1",selection,selectionArgs);
break;
case Demo1_ITEM:
String Id = uri.getPathSegments().get(1);
deletedRows = db.delete("Demo1","id = ?",new String[]{Id});
break;
default:
break;
}
return deletedRows;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case Demo1_DIR:
return "vnd.android.cursor.dir/vnd.com.example.revise_sqlitedatabase.Demo1";
case Demo1_ITEM:
return "vnd.android.cursor.item/vnd.com.example.revise_sqlitedatabase.Demo1";
default:
break;
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri uriReturn = null;
switch (uriMatcher.match(uri)){
case Demo1_DIR:
case Demo1_ITEM:
long newId = db.insert("Demo1",null,values);
uriReturn = Uri.parse("content://"+AUTHORITY+"/Demo1/"+newId);
break;
default:
break;
}
return uriReturn;
}
@Override
public boolean onCreate() {
db = LitePal.getDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Cursor cursor = null;
switch (uriMatcher.match(uri)){
case Demo1_DIR:
cursor = db.query("Demo1",projection,selection,selectionArgs,null,null,sortOrder);
break;
case Demo1_ITEM:
String Id = uri.getPathSegments().get(1);
cursor = db.query("Demo1",projection,"id = ?",new String[]{Id},null,null,sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int updatedRows = 0;
switch (uriMatcher.match(uri)){
case Demo1_DIR:
updatedRows = db.update("Demo1",values,selection,selectionArgs);
break;
case Demo1_ITEM:
String Id = uri.getPathSegments().get(1);
updatedRows = db.update("Demo1",values,"id = ?",new String[]{Id});
break;
default:
break;
}
return updatedRows;
}
}
二.访问刚才的程序中的数据
新建一个项目,我的如下,布局文件中有四个按钮,设置点击事件来访问数据
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="query data"/>
<Button
android:id="@+id/updata_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Up data"/>
<Button
android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="delete data"/>
</LinearLayout>
package com.example.contentproviderdemotest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private String mewId;
Uri uri = Uri.parse("content://com.example.revise_sqlitedatabase.provider/Demo1");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addData = (Button)findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues values = new ContentValues();
values.put("name","张三");
values.put("age",10);
Uri newUri = getContentResolver().insert(uri,values);
mewId = newUri.getPathSegments().get(1);
}
});
Button query = (Button)findViewById(R.id.query_data);
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor cursor = getContentResolver().query(uri,null,null,null,null);
if(cursor!=null){
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
Log.d("MainActivity",name);
Log.d("MainActivity",age);
}
cursor.close();
}
}
});
Button updata = (Button)findViewById(R.id.updata_data);
updata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues values = new ContentValues();
values.put("name","李四");
getContentResolver().update(uri,values,null,null);
}
});
Button delete = (Button)findViewById(R.id.delete_data);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getContentResolver().delete(uri,null,null);
}
});
}
}
运行后打印结果如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b94f36e769c8658103ff6f8bf2698f62.png)