你的位置:首页 > 操作系统

[操作系统]Android 内容提供者的实现


接着上文《Android 内容提供者简介》进一步实现内容提供者。

每个Content Provider类都使用URI(Universal Resource Identifier,通用资源标识符)作为独立的标识,格式如:content://com.example.app.provider/table1。其他应用程序通过不同的uri访问不同的内容提供者,并获取/操作里面的数据。

例如在本项目中对应如下URI:

content://com.wuyudong.db.personprovider/insert 添加的操作
content://com.wuyudong.db.personprovider/delete 删除的操作
content://com.wuyudong.db.personprovider/update 更新的操作
content://com.wuyudong.db.personprovider/query 查询的操作

在PersonDBProvider.java中添加代码:

package com.wuyudong.db;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 PersonDBProvider extends ContentProvider {  // 定义一个URI的匹配器用于匹配uri, 如果路径不满足条件 返回-1  private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  private static final int INSERT = 1;  private static final int DELETE = 2;  private static final int UPDATE = 3;  private static final int QUERY = 4;  private PersonSQLiteOpenHelper helper;   static {    // 添加一组匹配规则 com.wuyudong.db.personprovider    matcher.addURI("com.wuyudong.db.personprovider", "insert",        INSERT);    matcher.addURI("com.wuyudong.db.personprovider", "delete",        DELETE);    matcher.addURI("com.wuyudong.db.personprovider", "update",        UPDATE);    matcher.addURI("com.wuyudong.db.personprovider", "query", QUERY);  }  @Override  public boolean onCreate() {    helper = new PersonSQLiteOpenHelper(getContext());    return false;  }  @Override  public Cursor query(Uri uri, String[] projection, String selection,      String[] selectionArgs, String sortOrder) {    if (matcher.match(uri) == QUERY) {      // 返回查询的结果集      SQLiteDatabase db = helper.getReadableDatabase();      Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);      return cursor;    } else {      throw new IllegalArgumentException("路径不匹配,不能执行查询操作");    }  }  @Override  public String getType(Uri uri) {    // TODO Auto-generated method stub    return null;  }  @Override  public Uri insert(Uri uri, ContentValues values) {    // TODO Auto-generated method stub    return null;  }  @Override  public int delete(Uri uri, String selection, String[] selectionArgs) {    // TODO Auto-generated method stub    return 0;  }  @Override  public int update(Uri uri, ContentValues values, String selection,      String[] selectionArgs) {    // TODO Auto-generated method stub    return 0;  }}

新建一个名为other的项目,布局如下:

<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context=".MainActivity" >  <Button    android:onClick="click"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="读取DB的数据" /></RelativeLayout>

点击下图的按钮,kill掉 com.wuyudong.db进程

点击Button按钮后,com.wuyudong.db进程重新运行,而且打印数据库中person表中的数据

进一步完善其他操作,修改布局

<LinearLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context=".MainActivity" >  <Button    android:onClick="click"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="读取DB的数据" />   <Button    android:onClick="delete"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="删除DB的数据" />   <Button    android:onClick="update"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="修改DB的数据" />    <Button    android:onClick="insert"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="添加DB的数据" /></LinearLayout>

接下来实现PersonDBProvider中的其他方法

package com.wuyudong.db;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 PersonDBProvider extends ContentProvider {  // 定义一个URI的匹配器用于匹配uri, 如果路径不满足条件 返回-1  private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  private static final int INSERT = 1;  private static final int DELETE = 2;  private static final int UPDATE = 3;  private static final int QUERY = 4;  private PersonSQLiteOpenHelper helper;  static {    // 添加一组匹配规则 com.wuyudong.db.personprovider    matcher.addURI("com.wuyudong.db.personprovider", "insert", INSERT);    matcher.addURI("com.wuyudong.db.personprovider", "delete", DELETE);    matcher.addURI("com.wuyudong.db.personprovider", "update", UPDATE);    matcher.addURI("com.wuyudong.db.personprovider", "query", QUERY);  }  @Override  public boolean onCreate() {    helper = new PersonSQLiteOpenHelper(getContext());    return false;  }  @Override  public Cursor query(Uri uri, String[] projection, String selection,      String[] selectionArgs, String sortOrder) {    if (matcher.match(uri) == QUERY) {      // 返回查询的结果集      SQLiteDatabase db = helper.getReadableDatabase();      Cursor cursor = db.query("person", projection, selection,          selectionArgs, null, null, sortOrder);      return cursor;    } else {      throw new IllegalArgumentException("路径不匹配,不能执行查询操作");    }  }  @Override  public String getType(Uri uri) {    // TODO Auto-generated method stub    return null;  }  @Override  public Uri insert(Uri uri, ContentValues values) {    if (matcher.match(uri) == INSERT) {      // 返回查询的结果集      SQLiteDatabase db = helper.getWritableDatabase();      db.insert("person", null, values);    } else {      throw new IllegalArgumentException("路径不匹配,不能执行插入操作");    }    return null;  }  @Override  public int delete(Uri uri, String selection, String[] selectionArgs) {    if (matcher.match(uri) == DELETE) {      // 返回查询的结果集      SQLiteDatabase db = helper.getWritableDatabase();      db.delete("person", selection, selectionArgs);    } else {      throw new IllegalArgumentException("路径不匹配,不能执行删除操作");    }    return 0;  }  @Override  public int update(Uri uri, ContentValues values, String selection,      String[] selectionArgs) {    if (matcher.match(uri) == UPDATE) {      // 返回查询的结果集      SQLiteDatabase db = helper.getWritableDatabase();      db.update("person", values, selection, selectionArgs);    } else {      throw new IllegalArgumentException("路径不匹配,不能执行修改操作");    }    return 0;  }}