ContentProvider的用法

     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

	}

}


SqliteOpenHelper是一个数据库的帮助类,我们需要继承它去实现一个数据库,它有两个回调方法,oncreate和onupdate,oncreate不是马上就调用,它采用了懒加载原理,再第一次调用getWritableDatabase或getReadDatabase时,采用调用,这样更节约内存,试想如果我们没有对数据库做处理的话,就没有建数据库,节省了内存。这个机制在J2EE开发中也采用这种思想,比如Hibernate也有一个懒加载机制。onUpgrade方法是需要更新数据库的时候才调用,当version的版本变高的时候会更新。

   需要继承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>

源码下载


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值