目录
简述
ContentProvider是android的四大组件之一,主要是实现各个应用程序之间的(跨应用)数据共享。
示例
实现ContentProvider的APP
首先创建一个我们自己的OpenHelper类DBopenHelper,来创建一个数据库,并重写onCreate()方法,创建一张test表。
接着创建我们自定义的ContentProvider,设置ContentProvider的URI,实现onCreate(),根据需求重写对应的增删改查方法;这里,我重写了insert()方法,并通过另一个APP实现Content Provider来插入一条数据。
package com.example.myproviderapp.provider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myproviderapp.DBOpenHelper;
public class Myprovider extends ContentProvider {
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private DBOpenHelper dbOpenHelper;
static {
matcher.addURI("com.example.myproviderapp.provider","test",1);
}
@Override
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(this.getContext(), "test.db", null, 1);
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
return null;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
switch(matcher.match(uri))
{
case 1:
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
long rowId = db.insert("test", null, contentValues);
if(rowId > 0)
{
//在前面已有的Uri后面追加ID
Uri nameUri = ContentUris.withAppendedId(uri, rowId);
//通知数据已经发生改变
getContext().getContentResolver().notifyChange(nameUri, null);
return nameUri;
}
}
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
return 0;
}
}
创建完我们的provider后,一定要在Manifest文件里注册我们的provider。
我们的主页面将完成一个查询功能,即当Resolver插入一条数据后,在此APP内查询结果并展示。很容易完成,直接放代码:
package com.example.myproviderapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btselect;
private DBOpenHelper myhelper;
private Context mcontext;
private StringBuilder sb;
private ScrollView scrollView;
private TextView txt;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext=MainActivity.this;
myhelper =new DBOpenHelper(mcontext,"song_db",null,1);
scrollView =(ScrollView)findViewById(R.id.scrollView1);
txt=(TextView)findViewById(R.id.textView1);
btselect=(Button) findViewById(R.id.button);
btselect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db=myhelper.getWritableDatabase();
sb=new StringBuilder();
Cursor cursor=db.query("test",null,null,null,null,null,null);
while(cursor.moveToNext())
{
@SuppressLint("RANGE") Integer id=cursor.getInt(cursor.getColumnIndex("_id"));
@SuppressLint("RANGE") String name=cursor.getString(cursor.getColumnIndex("name"));
sb.append("ID: "+id+" "+"NAME: "+name+"\n");
}
sb.toString();
txt.setText(sb);
System.out.println(sb);
}
});
}
}
好了,第一个APP已经完成,接下来实现另一个APP。
实现ContentResolver的APP
创建一个新的项目,来实现ContentResolver的部分,通过editText和Button点击插入一条数据:
package com.example.myproviderapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btselect;
private DBOpenHelper myhelper;
private Context mcontext;
private StringBuilder sb;
private ScrollView scrollView;
private TextView txt;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext=MainActivity.this;
myhelper =new DBOpenHelper(mcontext,"song_db",null,1);
scrollView =(ScrollView)findViewById(R.id.scrollView1);
txt=(TextView)findViewById(R.id.textView1);
btselect=(Button) findViewById(R.id.button);
btselect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db=myhelper.getWritableDatabase();
sb=new StringBuilder();
Cursor cursor=db.query("test",null,null,null,null,null,null);
while(cursor.moveToNext())
{
@SuppressLint("RANGE") Integer id=cursor.getInt(cursor.getColumnIndex("_id"));
@SuppressLint("RANGE") String name=cursor.getString(cursor.getColumnIndex("name"));
sb.append("ID: "+id+" "+"NAME: "+name+"\n");
}
sb.toString();
txt.setText(sb);
System.out.println(sb);
}
});
}
}
结果演示
首先运行ContentProvider的APP,先查询一下数据库
接着运行ContentResolver的APP,插入两条数据 alex和Shawn
我们在此打开第一个APP,查询数据库,可以查询到插入进来数据,因此,这个示例也展示了ContentProvider在不同应用之间的数据共享。