ContentProvider使用完整示例(一)――自定义ContentProvider(二)

2014-11-24 12:57:10 · 作者: · 浏览: 2
rTest如下:

package cn.testcontentprovider;
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;
/**
 * 注意事项:
 * 在AndroidManifest.xml中注册ContentProvider时的属性
 * android:exported="true"表示允许其他应用访问.
 * 这样TestBaidu这个应用才可以访问该处的ContentProvider
 */
public class ContentProviderTest extends ContentProvider {
	private DBOpenHelper dbOpenHelper;
	private  UriMatcher URI_MATCHER;
	private static final int PERSONS = 0;
	private static final int PERSON = 1;
	
	@Override
	public boolean onCreate() {
		initUriMatcher();
		dbOpenHelper=new DBOpenHelper(getContext());
		return true;
	}
	//初始化UriMatcher
	private void initUriMatcher(){
		URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
		//表示返回所有的person,其中PERSONS为该特定Uri的标识码
		URI_MATCHER.addURI("cn.bs.testcontentprovider","person", PERSONS);
		//表示返回某一个person,其中PERSON为该特定Uri的标识码
		URI_MATCHER.addURI("cn.bs.testcontentprovider","person/#", PERSON);
	}

	/**
	 * 插入操作:
	 * 插入操作只有一种可能:向一张表中插入
	 * 返回结果为新增记录对应的Uri
	 * 方法db.insert()返回结果为新增记录对应的主键值
	 */
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
		switch (URI_MATCHER.match(uri)) {
		case PERSONS:
           long rowid=db.insert("person", "name,phone,salary", values);
           return ContentUris.withAppendedId(uri, rowid);
		default:
			throw new IllegalArgumentException("unknown uri"+uri.toString());
		}
	}
	
	/**
	 * 更新操作:
	 * 更新操作有
两种可能:更新一张表或者更新某条数据 * 在更新某条数据时原理类似于查询某条数据,见下. */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); int updataNum=0; switch (URI_MATCHER.match(uri)) { //更新表 case PERSONS: updataNum=db.update("person", values, selection, selectionArgs); break; //按照id更新某条数据 case PERSON: long id=ContentUris.parseId(uri); String where="personid="+id; if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } updataNum=db.update("person", values, where, selectionArgs); break; default: throw new IllegalArgumentException("unknown uri"+uri.toString()); } return updataNum; } /** * 删除操作: * 删除操作有两种可能:删除一张表或者删除某条数据 * 在删除某条数据时原理类似于查询某条数据,见下. */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); int deletedNum=0; switch (URI_MATCHER.match(uri)) { //删除表 case PERSONS: deletedNum=db.delete("person", selection, selectionArgs); break; //按照id删除某条数据 case PERSON: long id=ContentUris.parseId(uri); String where="personid="+id; if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } deletedNum=db.delete("person", where, selectionArgs); break; default: throw new IllegalArgumentException("unknown uri"+uri.toString()); } return deletedNum; } /** * 查询操作: * 查询操作有两种可能:查询一张表或者查询某条数据 * 注意事项: * 在查询某条数据时要注意--因为此处是按照personid来查询 * 某条数据,但是同时可能还有其他限制.例如