ContentProvider详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ContentProvider详解相关的知识,希望对你有一定的参考价值。
作用:把一个App中的数据库通过Url的形式共享出来,供其他App使用。
首先在App1中创建一个数据库,用SQLiteOpenHelper
1 public class MyOpenHelper extends SQLiteOpenHelper { 2 3 public MyOpenHelper(Context context){ 4 super(context,"Account.db",null,1); 5 } 6 @Override 7 public void onCreate(SQLiteDatabase db) { 8 db.execSQL("create table info(_id integer primary key autoincrement, name varchar(20),money varchar(10))"); 9 db.execSQL("insert into info(name, money) values(?,?)",new String[]{"张三","5000"}); 10 ContentValues cv = new ContentValues(); 11 cv.put("name","李四"); 12 cv.put("money","6000"); 13 db.insert("info",null,cv); 14 } 15 @Override 16 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 17 } 18 }
创建数据库:
1 MyOpenHelper myOpenHelper = new MyOpenHelper(this); 2 myOpenHelper.getReadableDatabase();
然后用过ContentProvider把数据库共享出来,首先看代码:
public class MyProvider extends ContentProvider { //定义一个Uri路径匹配器 (匹配不成功的时候会返回一个NO_MATCH返回一个-1) private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); private static final int QUERYSUCESS = 0; private static final int INSERTSUCESS = 1; private static final int UPADTESUCESS = 2; private static final int DELETESUCESS = 3; //创建一个静态代码块,在这里添加Uri static { /** * authority 注意:和清单文件里面定义的一样 com.coderwei.provider/query */ sURIMatcher.addURI("com.coderwei.provider","query",QUERYSUCESS ); sURIMatcher.addURI("com.coderwei.provider","insert",INSERTSUCESS ); sURIMatcher.addURI("com.coderwei.provider","update",UPADTESUCESS ); sURIMatcher.addURI("com.coderwei.provider","delete",DELETESUCESS ); } private MyOpenHelper myOpenHelper; @Override public boolean onCreate() { //初始化MyOpenHelper myOpenHelper = new MyOpenHelper(getContext()); return false; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if (sURIMatcher.match(uri)==QUERYSUCESS){ //说明路径匹配成功 SQLiteDatabase db = myOpenHelper.getReadableDatabase(); Cursor cursor = db.query("info",projection,selection,selectionArgs,null,null,sortOrder); //db.close(); //这里不能关闭Cursor return cursor; }else { throw new IllegalArgumentException("uri路径不匹配"); } } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { if (sURIMatcher.match(uri)==INSERTSUCESS){ SQLiteDatabase db = myOpenHelper.getReadableDatabase(); long insert = db.insert("info",null,values); Uri uri1 = Uri.parse("com.coderwei.insert/"+insert); //db.close(); return uri1; }else { throw new IllegalArgumentException("uri路径不匹配"); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { if (sURIMatcher.match(uri)==DELETESUCESS){ SQLiteDatabase db = myOpenHelper.getReadableDatabase(); int delete = db.delete("info",selection,selectionArgs); //db.close(); return delete; }else { throw new IllegalArgumentException("uri路径不匹配"); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { if (sURIMatcher.match(uri)== UPADTESUCESS){ SQLiteDatabase db = myOpenHelper.getReadableDatabase(); int update = db.update("info",values,selection,selectionArgs); //db.close(); return update; }else { throw new IllegalArgumentException("uri路径不匹配"); } } }
代码很长,但是很简单,主要把上面四个增删改查方法实现就行,上面的db.close()关闭的时候报错了,所以就注释掉了,关不关都可以。
Insert方法返回的是一个Url对象,直接自己写一个就行了。
记得在Manifest.xml中配置ContentProvider:
<provider android:authorities="com.coderwei.provider" android:exported="true" android:name=".MyProvider"> </provider>
android:authorities 自己随便给一个字符串,
android:exported="true"一定要写上这句,这句话表示可以被另一个Application的组件启动,没写可能会报错。
sURIMatcher.addURI("com.coderwei.provider","query",QUERYSUCESS );中的com.coderwei.provider就是这个字符串。
然后就是来到App2了,先看代码:
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 } 8 public void click1(View v){ 9 doQuery(); 10 } 11 public void click2(View v){ 12 doInsert(); 13 } 14 public void click3(View v){ 15 doDelete(); 16 } 17 public void click4(View v){ 18 doUpdate(); 19 } 20 21 private void doQuery(){ 22 Uri uri = Uri.parse("content://com.coderwei.provider/query"); 23 Cursor cursor = getContentResolver().query(uri,new String[]{"name","money"},null,null,null); 24 25 if (cursor!=null){ 26 while (cursor.moveToNext()){ 27 String name = cursor.getString(0); 28 String money = cursor.getString(1); 29 System.out.println("name:"+name+" money: "+money); 30 } 31 } 32 } 33 34 private void doInsert(){ 35 Uri uri = Uri.parse("content://com.coderwei.provider/insert"); 36 ContentValues cv = new ContentValues(); 37 cv.put("name","王五"); 38 cv.put("money","800"); 39 Uri uri1 = getContentResolver().insert(uri,cv); 40 System.out.println("插入uri1:"+uri1); 41 } 42 43 private void doDelete(){ 44 Uri uri = Uri.parse("content://com.coderwei.provider/delete"); 45 int delete = getContentResolver().delete(uri,"name=?",new String[]{"王五"}); 46 System.out.print("刪除了: "+delete); 47 } 48 49 private void doUpdate(){ 50 Uri uri = Uri.parse("content://com.coderwei.provider/update"); 51 ContentValues cv = new ContentValues(); 52 cv.put("money","9000"); 53 int update = getContentResolver().update(uri,cv,"name=?",new String[]{"王五"}); 54 System.out.println("更新了:"+update); 55 } 56 }
里面就是四个增删改查方法,就Query稍微麻烦一点,查询的结果返回一个Cursor对象,然后把这个Cursor对象的内容取出来就行了,
以上是关于ContentProvider详解的主要内容,如果未能解决你的问题,请参考以下文章
ContentProvider启动过程and多进程调用全过程源码详解