ContentProvider简介
内容提供者( ContentProvider ) 主要用于不同应用程序之间的数据共享,并且提供CRUD,内容提供者一般提供两种用法:
1 第一种使用现有的内容提供器来读取和操作数据比如: 查询 音乐 , 视频 , 联系人等等一些信息;
2 第二种就是实现自己的内容提供器给我们的程序提供外界的接口
下面我会用一个我写好的一个类来给大家介绍基本的方法 :
package com.example.liangshaoteng.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.widget.Toast;
/**
* Created by liangshaoteng on 17-6-21.
*/
public class MyContentProvider extends ContentProvider {
public static final int TABLE1_DIR = 0;
public static final int TABLE1_ITEM = 1;
public static final int TABLE2_DIR = 2;
public static final int TABLE2_ITEM = 3;
//一般都是用我们的包名
public static final String uriPath = "com.example.liangshaoteng.contentprovider";
private static UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(uriPath, "table1", TABLE1_DIR);
uriMatcher.addURI(uriPath, "table1/#", TABLE1_ITEM);
uriMatcher.addURI(uriPath, "table2", TABLE2_DIR);
uriMatcher.addURI(uriPath, "table2/#", TABLE2_ITEM);
}
@Override
public boolean onCreate() {
return false;
}
/**
* @param uri //用来查询那张表
* @param projection //用来查询那些列
* @param selection
* @param selectionArgs //用于查询那些行
* @param sortOrder //用于对结果进行排序
* @return
*/
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
Toast.makeText(getContext(), "查询table1表中所有的数据", Toast.LENGTH_LONG).show();
break;
case TABLE1_ITEM:
Toast.makeText(getContext(), "查询table1表中单条的数据", Toast.LENGTH_LONG).show();
break;
case TABLE2_DIR:
Toast.makeText(getContext(), "查询table2表中所有的数据", Toast.LENGTH_LONG).show();
break;
case TABLE2_ITEM:
Toast.makeText(getContext(), "查询table2表中单条的数据", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return null;
}
/**
* @param uri //根据传来的uri返回相应的MIME
* @return
*/
@Nullable
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
return "vnd.android.cursor.dir/vnd.com.example.liangshaoteng.contentprovider.table1";
case TABLE1_ITEM:
return "vnd.android.cursor.item/vnd.com.example.liangshaoteng.contentprovider.table1";
case TABLE2_DIR:
return "vnd.android.cursor.dir/vnd.com.example.liangshaoteng.contentprovider.table2";
case TABLE2_ITEM:
return "vnd.android.cursor.item/vnd.com.example.liangshaoteng.contentprovider.table2";
default:
break;
}
return null;
}
/**
* @param uri //确定添加到那张表
* @param values //待添加的数据保存在values
* @return //返回一条表示新添加数据的uri
*/
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
/**
* @param uri //用来删除那张表
* @param selection //用来约束删除那些行
* @param selectionArgs
* @return //并返回删除的行数
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
/**
* @param uri //用于更新那张表
* @param values //新数据保存在values
* @param selection
* @param selectionArgs //约束更新哪一行
* @return
*/
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
对于访问现有的内容提供器我就不多说了,就给大家看一下我写的一个CRUD的代码看着挺多其实挺简单的,有什么不懂得或者有不同意见或建议的请给出指示谢谢!
首先给大家看一下效果图:
下面我们需要创建一张表:
package com.example.liangshaoteng.contentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* Created by liangshaoteng on 17-6-21.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table Book(_id integer primary key autoincrement,author,price,pages,name)");
Toast.makeText(context, "创建成功!", Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
其次就是创建我们的内容提供器:
记得要在manifest里面注册!
下面我就简单的写的具体还需要你根据项目需求来写
package com.example.liangshaoteng.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class DataContentProvider extends ContentProvider {
public static final int TABLE1_DIR = 0;
public static final int TABLE1_ITEM = 1;
public static final int TABLE2_DIR = 2;
public static final int TABLE2_ITEM = 3;
public static final String AUTHORITY = "com.example.liangshaoteng.contentprovider";
private static UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "book", TABLE1_DIR);
uriMatcher.addURI(AUTHORITY, "book/#", TABLE1_ITEM);
uriMatcher.addURI(AUTHORITY, "category", TABLE2_DIR);
uriMatcher.addURI(AUTHORITY, "category/#", TABLE2_ITEM);
}
private MyDatabaseHelper myDatabaseHelper;
public DataContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();
int deleteRows = 0;
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
deleteRows = writableDatabase.delete("Book", selection, selectionArgs);
break;
case TABLE1_ITEM:
String sBook = uri.getPathSegments().get(1);
deleteRows = writableDatabase.delete("Book", "id=?", new String[]{sBook});
break;
case TABLE2_DIR:
deleteRows = writableDatabase.delete("Category", selection, selectionArgs);
break;
case TABLE2_ITEM:
String sCategory = uri.getPathSegments().get(1);
deleteRows = writableDatabase.delete("Category", "id=?", new String[]{sCategory});
break;
default:
break;
}
return deleteRows;
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
return "vnd.android.cursor.dir/vnd.com.example.liangshaoteng.contentprovider.book";
case TABLE1_ITEM:
return "vnd.android.cursor.item/vnd.com.example.liangshaoteng.contentprovider.book";
case TABLE2_DIR:
return "vnd.android.cursor.dir/vnd.com.example.liangshaoteng.contentprovider.category";
case TABLE2_ITEM:
return "vnd.android.cursor.item/vnd.com.example.liangshaoteng.contentprovider.category";
default:
break;
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
case TABLE1_ITEM:
long bookBook = writableDatabase.insert("Book", null, values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/book/" + bookBook);
break;
case TABLE2_DIR:
case TABLE2_ITEM:
long bookCategory = writableDatabase.insert("Category", null, values);
uriReturn = Uri.parse("content://" + AUTHORITY + "/category/" + bookCategory);
break;
default:
break;
}
return uriReturn;
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
myDatabaseHelper = new MyDatabaseHelper(getContext(), "BookStores.db", null, 1);
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.
SQLiteDatabase readableDatabase = myDatabaseHelper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
cursor = readableDatabase.query("Book", projection, selection, selectionArgs, null, null, sortOrder);
break;
case TABLE1_ITEM:
String s = uri.getPathSegments().get(1);
cursor = readableDatabase.query("Book", projection, "id=?", new String[]{s}, null, null, sortOrder);
break;
case TABLE2_DIR:
cursor = readableDatabase.query("Category", projection, selection, selectionArgs, null, null, sortOrder);
break;
case TABLE2_ITEM:
String s1 = uri.getPathSegments().get(1);
cursor = readableDatabase.query("Category", projection, "id=?", new String[]{s1}, null, null, sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
SQLiteDatabase writableDatabase = myDatabaseHelper.getWritableDatabase();
int updatedRows = 0;
switch (uriMatcher.match(uri)) {
case TABLE1_DIR:
updatedRows = writableDatabase.update("Book", values, selection, selectionArgs);
break;
case TABLE1_ITEM:
String sBook = uri.getPathSegments().get(1);
updatedRows = writableDatabase.update("Book", values, "id=?", new String[]{sBook});
break;
case TABLE2_DIR:
updatedRows = writableDatabase.update("Category", values, selection, selectionArgs);
break;
case TABLE2_ITEM:
String sCategory = uri.getPathSegments().get(1);
updatedRows = writableDatabase.update("Category", values, "id=?", new String[]{sCategory});
break;
default:
break;
}
return updatedRows;
}
}
好了基本上就是这样了! 不足之处还望指出 !