现在的位置: 首页 > 综合 > 正文

使用ContentProvider共享数据

2013年08月28日 ⁄ 综合 ⁄ 共 5240字 ⁄ 字号 评论关闭

        当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。以前我们学习过文件的操作模式,通过指定文件的操作模式为Context.MODE_WORLD_READABLE 或Context.MODE_WORLD_WRITEABLE同样可以对外共享数据,但数据的访问方式会因数据存储的方式而不同,如:采用xml文件对外共享数据,需要进行xml解析来读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences
API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。

当应用需要通过ContentProvider对外共享数据时,第一步需要继承ContentProvider并重写下面方法:

 public class PersonContentProvider extends ContentProvider{

   public boolean onCreate()//该方法在ContentProvider创建后就会被调用, Android开机后, ContentProvider在其它应用第一次访问它时才会被创建。
   public Uri insert(Uri uri, ContentValues values)//该方法用于供外部应用往ContentProvider添加数据。
   public int delete(Uri uri, String selection, String[] selectionArgs)//该方法用于供外部应用从ContentProvider删除数据。
   public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)//该方法用于供外部应用更新ContentProvider中的数据。
   public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)//该方法用于供外部应用从ContentProvider中获取数据。
   public String getType(Uri uri)}

 该方法用于返回当前Url所代表数据的MIME类型。如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头,例如:要得到所有person记录的Uri为

content://cn.itcast.provider.personprovider/person,那么返回的MIME类型字符串应该为:“vnd.android.cursor.dir/person”。如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头,例如:得到id为10的person记录,Uri为

content://cn.itcast.provider.personprovider/person/10,那么返回的MIME类型字符串应该为:“vnd.android.cursor.item/person”。

第二步需要在AndroidManifest.xml使用<provider>对该ContentProvider进行配置,为了能让其他应用找到该ContentProvider , ContentProvider 采用了authorities(主机名/域名)对它进行唯一标识,你可以把 ContentProvider看作是一个网站(想想,网站也是提供数据者),authorities 就是他的域名:

<manifest .... >
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <provider android:name=".PersonContentProvider" android:authorities="cn.itcast.providers.personprovider"/>
    </application>
</manifest>
注意:一旦应用继承了ContentProvider类,后面我们就会把这个应用称为ContentProvider(内容提供者)。

package cn.itcast.db;

import cn.itcast.service.DBOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class PersonContentProvider extends ContentProvider {
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
private DBOpenHelper dbOpenHelper;
static{
matcher.addURI("cn.itcast.providers.personprovider", "person", PERSONS);
matcher.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);
}

@Override
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(this.getContext());
return true;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0 ;//已经删除的记录数量
switch (matcher.match(uri)) {
case PERSONS:
num = db.delete("person", selection, selectionArgs);
break;

case PERSON:long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
where = where + " and "+ selection;
}
num = db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}

getContext().getContentResolver().notifyChange(uri, null);
return num;
}

@Override
public String getType(Uri uri) {//返回当前操作的数据类型
switch (matcher.match(uri)) {
case PERSONS://操作的是集合类型数据
return "vnd.android.cursor.dir/person";
case PERSON:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
}

@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
long id = 0 ;
switch (matcher.match(uri)) {
case PERSONS:
id = db.insert("person", "personid", values);//得到记录的id
getContext().getContentResolver().notifyChange(uri, null);

 return ContentUris.withAppendedId(uri, id);//返回代表新增记录的Uri
case PERSON:
id = db.insert("person", "personid", values);//得到记录的id
String strUri = uri.toString();

Uri personUri = Uri.parse(strUri.substring(0, strUri.lastIndexOf("/")));
getContext().getContentResolver().notifyChange(personUri, null);
return ContentUris.withAppendedId(personUri, id);
default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (matcher.match(uri)) {
case PERSONS:
return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);

case PERSON:

long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
where = where + " and "+ selection;
}
return db.query("person", projection, where, selectionArgs, null, null, sortOrder);

default:
throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0 ;//已经修改的记录数量
switch (matcher.match(uri)) {
case PERSONS:
num = db.update("person", values, selection, selectionArgs);
break;

case PERSON:long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){ 
where = where + " and "+ selection;
}
num = db.update("person", values, where, selectionArgs);
break;
default:

throw new IllegalArgumentException("Unkown Uri:"+ uri);
}
getContext().getContentResolver().notifyChange(uri, null);//通知数据发生变化
return num;
}
}

抱歉!评论已关闭.