contentProvider的知识

什么是contentProvider

1.contentProvider是Android四大组件之一。
2.contentProvider是一种数据包装器发布者,它提供统一的接口对数据进行操作,调用者不用关心数据到底是如何存储的
3.contentProvider主要用于不同程序间,不同APP间的数据共享。

什么是contentResolver

1.contentResolver是数据调用者,contentProvider将数据发布出来后,通过contentResolver对象结合Uril进行调用。

2.通过contentResolver可以调用contentProvider的增删改查操作。

什么是Uril

1.通用资源标识符,简称Uril

2.Uril代表要操作数据的地址,每个contentProvider发布数据时都有一个唯一的地址。

 switch (view.getId()){
            case R.id.test_btn:
                String name = nameEt.getText().toString();

                Uri uri=Uri.parse("content://myapplication.com.example.anew.entity.MyProvider");
                ContentResolver resolver=getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", name);
                resolver.insert(uri,values);
                break;
   注意:在调用contentProvider前,必须确定Uril

自定义contentProvider的步骤

1.使用SQLite技术,创建数据库和数据表
2.新建类继承contentProvider
3.创建UrilMatcher,定义Uril规则
4.重写留个抽象方法
5.在manifest里面添加provider
6.ContentResolver 对contentProvider共享的数据进行增删改查

 <provider
            android:name=".entity.MyProvider"
            android:authorities="myapplication.com.example.anew.entity.MyProvider"
            android:enabled="true"
            android:exported="true" />
public class MyProvider extends ContentProvider {


    private String TAG = "MyProvider";

    @Override
    public boolean onCreate() {
        Log.e(TAG, "onCreate^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {

        Log.e(TAG, "query^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.e(TAG, "getType^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.e(TAG, "insert^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");


        DbHelper dbHelper = new DbHelper(getContext(), "student_db", null, 1);
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.insert("student", null, values);
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {

        Log.e(TAG, "delete^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");


        DbHelper dbHelper = new DbHelper(getContext(), "student_db", null, 1);
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.delete("student", s, strings);

        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        Log.e(TAG, "update^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        return 0;
    }
}

然后在另一个工程里面加上一个测试的按钮。添加点击事件,可以打印一下,发现两个工程之间进行通信了。数据也添加成功了。其他的删改查都是差不多的操作。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button testBtn;
    private Button delBtn;
    private EditText  nameEt;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindID();
    }

    private void bindID() {
        testBtn=findViewById(R.id.test_btn);
        delBtn=findViewById(R.id.del_btn);
        nameEt=findViewById(R.id.main_et);
        nameEt.setOnClickListener(this);
        testBtn.setOnClickListener(this);
        delBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.test_btn:
                String name = nameEt.getText().toString();

                Uri uri=Uri.parse("content://myapplication.com.example.anew.entity.MyProvider");
                ContentResolver resolver=getContentResolver();
                ContentValues values = new ContentValues();
                values.put("name", name);
                resolver.insert(uri,values);
                break;

最后是xml的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="myapplication.com.example.teacherdemo.MainActivity">

    <EditText
        android:id="@+id/main_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/test_btn"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="测试一下"
        android:textSize="20sp" />


    <Button
        android:id="@+id/del_btn"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="删除"
        android:textSize="20sp" />

</LinearLayout>
     其实说白了contentProvider是用来跨APP通信的,uriMatcher是匹配URI的工具,用来对URI进行解析的,然后确定调用者需要执行那个操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值