一.什么是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>
效果图如下:
在使用ContentResolver方法的Activity添加数据在使用ContentProvider就会接受到。