ContentProvider的出现解决了跨进程访问数据的问题,其内部的原理主要是BINDER和匿名共享内存来实现的,它主要是由ActivitymanagerService提供管理,我们知道建立一个数据库只能由本应用访问,其他应用访问不了的,但是我们我们用contentprovider去包装数据库就可以通过uri去访问了。现在介绍一下怎么建立一个contentprovider。
首先我们需要建立一个数据库。
public class MyDatabaseOpenHelper extends SQLiteOpenHelper {
private final static int version=1;
private final static String name = "student.db";
private String sql = "CREATE TABLE student (id INTEGER PRIMARY KEY,"+
"name text, age INTEGER);";
public MyDatabaseOpenHelper(Context context) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
需要继承contentprovider类实现里面的oncreate,query,insert,update,delete,getType方法具体如下:
public class StudentProvider extends ContentProvider {
private MyDatabaseOpenHelper myDatabaseOpenHelper=null;
private SQLiteDatabase database;
private final static int STUDENTS = 1;
private final static int STUDENT =2;
private final static String AUTHORITY = "com.example.contentproviderdemo";
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, "student", STUDENTS);
sUriMatcher.addURI(AUTHORITY, "student/#", STUDENT);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
database = myDatabaseOpenHelper.getWritableDatabase();
int id = 0;
switch (sUriMatcher.match(uri)) {
case STUDENTS:
id = database.delete("student", selection, selectionArgs);
break;
case STUDENT:
if(selection==null||"".equals(selection)){
selection ="id = " + uri.getLastPathSegment();
}else{
selection = selection + " and id = " + uri.getLastPathSegment();
}
id = database.delete("student", selection, selectionArgs);
break;
}
return id;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
switch (sUriMatcher.match(uri)) {
case STUDENTS:
return "vnd.android.cursor.dir/student";
case STUDENT:
return "vnd.android.cursor.item/student";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
database = myDatabaseOpenHelper.getWritableDatabase();
Uri newUri=null;
switch (sUriMatcher.match(uri)) {
case STUDENTS:
long id = database.insert("student", null, values);
newUri = ContentUris.withAppendedId(uri, id);
break;
}
return newUri;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
myDatabaseOpenHelper = new MyDatabaseOpenHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
database = myDatabaseOpenHelper.getWritableDatabase();
Cursor cursor = null;
switch (sUriMatcher.match(uri)) {
case STUDENTS:
cursor = database.query("student", projection, selection, selectionArgs, null, null, null);
break;
case STUDENT:
if(selection==null||"".equals(selection)){
selection ="id = " + uri.getLastPathSegment();
}else{
selection = selection + " and id = " + uri.getLastPathSegment();
}
cursor = database.query("student", projection, selection, selectionArgs, null, null, null);
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
database = myDatabaseOpenHelper.getWritableDatabase();
int id =0;
switch (sUriMatcher.match(uri)) {
case STUDENTS:
id = database.update("student", values, selection, selectionArgs);
break;
case STUDENT:
if(selection==null||"".equals(selection)){
selection ="id = " + uri.getLastPathSegment();
}else{
selection = selection + " and id = " + uri.getLastPathSegment();
}
id = database.update("student", values, selection, selectionArgs);
break;
}
return id;
}
}
别忘了,还需要在AndroidManifest.xml里去注册contentProvider,它有个很重要的authorities属性,它是用来建立uri重要的一部分
<provider
android:name="StudentProvider"
android:authorities="com.example.contentproviderdemo"
>
</provider>
源码下载