ContentProvider和ContentResolver

 一.什么是ContentProvider和ContentResolver
1.ContentProvider是安卓四大组件之一。
2.ContentProvider是一种数据包装器(发布者),它提供统一的接口对数据进行操作,调用者不用关心数据到底是如何存储的。
3.ContentProvider主要用于应用程序间,不同APP间的数据共享。
4.ContentResolver是数据调用者,ContentProvider将数据发布出来后,通过ContentResolver对象结合Uri(全局资源标识符,代表要操作的数据地址)进行调用。
5.通过ContentResolver可以调用ContentProvider的增删改查操作。
6.url与uri的区别

```
url:http://baidu.com/index.html


uri:content://com.android.contacts/contacts
在调用ContentProvider前,必须先确定uri

```
二.为什么要用ContentProvider和ContentResolver
在实际开发中,经常有多款APP有直接交换数据需求,如微信读取手机通讯录,支付宝读取淘宝订单,这里就要用到ContentProvider和ContentResolver提供支持。
三.如何使用ContentProvider
1.使用SQLite技术,创建好数据库和数据表。
2.新建类继承ContentProvider。
3.创建UriMatcher定义uri规则。
4.重写6个方法(onCreate、getType、insert、update、delete、query)。
5.在安卓Mainfest中注册provider(四大组件都要注册)。
6.使用ContentResolver对ContentProvider共享数据中进行增删改查。
四.代码展示
首先在安卓Mainfest注册provider

```
<provider
            android:enabled="true"
            android:exported="true"
            android:authorities="com.ll.Mys.Rng"
            android:name=".contenprovider.MyProvider"/>
```
如何再新建类继承ContentProvider

```
package com.example.ll.myapplication.contenprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.example.ll.myapplication.db.DBhelper;

/**
 * Created by ll on 2018/3/29.
 */

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 contentValues) {
        Log.e(TAG, "insert...........");

        DBhelper dBhelper = new DBhelper(this.getContext(), "student_db", null, 1);
        SQLiteDatabase database = dBhelper.getWritableDatabase();

        database.insert("student", null, contentValues);
        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","name=?",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;
    }
}

```
最后在另一个APP中进行ContentResolver方法


```
package com.example.ll.king;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.net.URI;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button textbtn;
private Button addbtn;
private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BindId();
    }

    private void BindId() {
        textbtn=findViewById(R.id.btn_ll);
        textbtn.setOnClickListener(this);
        addbtn=findViewById(R.id.add_btn);
        addbtn.setOnClickListener(this);
        editText=findViewById(R.id.xiueru);
    }

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

                Uri uri=Uri.parse("content://com.ll.Mys.Rng");
                ContentResolver resolver=getContentResolver();
                ContentValues contentValues=new ContentValues();
                contentValues.put("name",name);
               resolver.insert(uri,contentValues);

                break;
            case  R.id.add_btn:
                String name1=editText.getText().toString();

                Uri uri1=Uri.parse("content://com.ll.Mys.Rng");
                ContentResolver resolver1=getContentResolver();

                resolver1.delete(uri1,"name=?",new String[]{name1});

                break;
        }

    }
}

```
xml中代码

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ll.king.MainActivity">
<EditText
    android:id="@+id/xiueru"
    android:layout_width="match_parent"
    android:layout_height="50dp" />
    <Button
        android:text="测试一下"
        android:id="@+id/btn_ll"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/add_btn"
        android:text="删除一条数据"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

```

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值