实例2
在自己的用户目录里边创建的数据库,自己提供provider,自己访问自己的数据库
效果
AndroidManifest.xml
MyContentProvider.java
package com.example.contentproviderdemo;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.IntentFilter;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;
public class MyContentProvider extends ContentProvider {
private DBHelper mDBHelper;
private SQLiteDatabase mDB;
public MyContentProvider() {
}
//定义为静态的,可以在静态域里边初始化
public static UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final String authority = "MyContentProvider";
private static final int PERSONS = 0x111;
private static final int PERSONS_ID = 0x222;
//静态域初始化
static {
//content://MyContentProvider/person
sUriMatcher.addURI(authority,"person",PERSONS);
//content://MyContentProvider/person/id
sUriMatcher.addURI(authority,"person/#",PERSONS_ID);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
//vnd.android.cursor.item/ 单记录
//vnd.android.cursor.dir/ 多记录
String type = null;
int code = sUriMatcher.match(uri);
switch (code) {
case PERSONS_ID:
type = "vnd.android.cursor.item/person";
break;
case PERSONS:
type = "vnd.android.cursor.dir/person";
break;
default:
break;
}
return type;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
mDB = mDBHelper.getReadableDatabase();
long id = mDB.insert("person",null,values);
//返回组合好的uri 类似://content://MyContentProvider/person/id
return ContentUris.withAppendedId(uri,id);
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
mDBHelper = new DBHelper(getContext());
return true ;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
Log.d("15pb","MyContentProvider::query");
mDB = mDBHelper.getReadableDatabase();
int code = sUriMatcher.match(uri);
if (code==PERSONS){
return mDB.query("person",new String[]{"_id","name"},null,null,null,null,null);
}else if(code==PERSONS_ID){
long id = ContentUris.parseId(uri);
return mDB.query("person",new String[]{"_id","name"},"_id=?",new String[]{""+id},null,null,null);
}
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
}
DBHelper.java
package com.example.contentproviderdemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(@Nullable Context context) {
super(context, "mydata.db", null, 1);
}
public DBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL("create table person(_id integer primary key autoincrement,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MainActivity.java
package com.example.contentproviderdemo;
import android.content.ContentResolver;
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 androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View view) {
// 1. 获取内容解析者
ContentResolver resolver = getContentResolver();
// 2. 创建URI
Uri uri = Uri.parse("content://"+"MyContentProvider");
// 3. 查询
Cursor cursor = resolver.query(uri,null,null,null,null,null);
Log.d("15pb","cursor:"+cursor);
}
public void btnClick1(View view) {
// 1. 获取内容解析者
ContentResolver resolver = getContentResolver();
// 2. 创建URI
Uri uri = Uri.parse("content://"+"MyContentProvider");
// 3. 创建数据
for (int i = 0; i < 100; i++) {
ContentValues values = new ContentValues();
values.put("_id",""+i);
values.put("name","王大锤"+i);
resolver.insert(uri,values);
}
//4.查询数据
Uri uri1 = Uri.parse("content://"+"MyContentProvider"+"/person");
Cursor cursor = resolver.query(uri1,null,null,null,null,null);
Log.d("15pb","cursor:"+cursor);
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
Log.d("15pb","id:"+id+"name="+name);
}
}
public void btnClick2(View view) {
// 1. 获取内容解析者
ContentResolver resolver = getContentResolver();
// 2. 创建URI
Uri uri = Uri.parse("content://"+"MyContentProvider");
// 3. 创建数据
for (int i = 0; i < 100; i++) {
ContentValues values = new ContentValues();
values.put("_id",""+i);
values.put("name","王大锤"+i);
resolver.insert(uri,values);
}
//4.查询数据
Uri uri1 = Uri.parse("content://"+"MyContentProvider"+"/person/10");
Cursor cursor = resolver.query(uri1,null,null,null,null,null);
Log.d("15pb","cursor:"+cursor);
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
Log.d("15pb","id:"+id+"name="+name);
}
}
}
activity_main.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:onClick="btnClick"
android:text="访问内容提供者"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/btn2"
android:onClick="btnClick1"
android:text="访问内容提供者-插入数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/btn3"
android:onClick="btnClick2"
android:text="访问内容提供者-查询指定数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
</LinearLayout>