Android开发之浅谈内容提供者provider的使用

大家新年好。博主刚回到广州就要各种忙。博主换了份工作,所以昨天一来到广州就忙着找房子,好在昨晚就已经搞定。时间比较紧迫,这篇文章就直接将如何使用provider了。

内容提供者一般用于进程之间通信,当一个app需要访问另外一个app的数据库内容的时候,就需要用到内容提供者。为了节约时间,博主直接偷懒上代码了,直接拿博主之前写过的一个短信项目来上了。

首先,定义一个内容提供者

package com.freedom.intelligencesms.provider;


import com.freedom.intelligencesms.db.GroupOpenHelper;
import com.freedom.intelligencesms.utils.SmsUtils;

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;
import android.text.TextUtils;

public class GroupContentProvider extends ContentProvider {
	//授权地址
	private static final String AUTHORITY = "com.freedom.intelligencesms.provider.GroupContentProvider";
	private static final int GROUPS_INSERT = 0;
	private static final int GROUPS_QUERY_ALL = 1;
	private static final int GROUPS_UPDATE = 2;
	private static final int THREAD_GROUP_INSERT = 3;
	private static final int THREAD_GROUP_QUERY_ALL = 4;
	private static final int THREAD_GROUP_QUERY = 5;
	private static final int GROUPS_DELETE = 6;
	private static UriMatcher uriMatcher;
	private GroupOpenHelper mOpenHelper;
	private final String GROUPS_TABLE = "groups";
	private final String THREAD_GROUP_TABLE = "thread_group";
	
	static {
		//定义一个匹配器
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/groups/insert
		uriMatcher.addURI(AUTHORITY, "groups/insert", GROUPS_INSERT);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/groups/
		uriMatcher.addURI(AUTHORITY, "groups", GROUPS_QUERY_ALL);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/groups/update
		uriMatcher.addURI(AUTHORITY, "groups/update", GROUPS_UPDATE);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/groups/delete/#
		uriMatcher.addURI(AUTHORITY, "groups/delete/#", GROUPS_DELETE);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/thread_group/insert
		uriMatcher.addURI(AUTHORITY, "thread_group/insert", THREAD_GROUP_INSERT);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/thread_group
		uriMatcher.addURI(AUTHORITY, "thread_group", THREAD_GROUP_QUERY_ALL);
		
		// content://com.freedom.intelligencesms.provider.GroupContentProvider/thread_group/#
		uriMatcher.addURI(AUTHORITY, "thread_group/#", THREAD_GROUP_QUERY);
	}

	@Override
	public boolean onCreate() {
		//拿到数据库
		mOpenHelper = GroupOpenHelper.getInstance(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		switch (uriMatcher.match(uri)) {
		case GROUPS_QUERY_ALL:		
			if(db.isOpen()) {
				Cursor cursor = db.query(GROUPS_TABLE, projection, selection, selectionArgs, null, null, sortOrder);
				cursor.setNotificationUri(getContext().getContentResolver(), SmsUtils.GROUPS_QUERY_ALL_URI);
				return cursor;
			}
			break;
		case THREAD_GROUP_QUERY_ALL:
			if(db.isOpen()) {
				return db.query(THREAD_GROUP_TABLE, projection, selection, selectionArgs, null, null, sortOrder);
			}
			break;
		case THREAD_GROUP_QUERY:
			if(db.isOpen()) {
				long id = ContentUris.parseId(uri);
				String where = "_id = " + id;
				if(!TextUtils.isEmpty(selection)) {
					where = selection + " and " + where;
				}
				return db.query(THREAD_GROUP_TABLE, projection, where, selectionArgs, null, null, sortOrder);
			}
			break;
		default:
			throw new IllegalArgumentException("Unknow Uri : " + uri);
		}
		return null;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		switch (uriMatcher.match(uri)) {
		case GROUPS_INSERT:		
			if(db.isOpen()) {
				long id = db.insert(GROUPS_TABLE, null, values);
				getContext().getContentResolver().notifyChange(SmsUtils.GROUPS_QUERY_ALL_URI, null);
				return ContentUris.withAppendedId(uri, id);
			}
			break;
		case THREAD_GROUP_INSERT:		
			if(db.isOpen()) {
				long id = db.insert(THREAD_GROUP_TABLE, null, values);
				return ContentUris.withAppendedId(uri, id);
			}
			break;
		default:
			throw new IllegalArgumentException("Unknow Uri : " + uri);
		}
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		switch (uriMatcher.match(uri)) {
		case GROUPS_DELETE:
			SQLiteDatabase db = mOpenHelper.getWritableDatabase();
			if(db.isOpen()) {
				long group_id = ContentUris.parseId(uri);
				String where = "_id = " + group_id;
				if(!TextUtils.isEmpty(selection)) {
					where = selection + " and " + where;
				}
				int count = db.delete(GROUPS_TABLE, where, selectionArgs);		
				getContext().getContentResolver().notifyChange(SmsUtils.GROUPS_QUERY_ALL_URI, null);				
				where = "group_id = " + group_id;
				db.delete(THREAD_GROUP_TABLE, where, null);	
				return count;
			}
			break;
		default:
			throw new IllegalArgumentException("Unknow Uri : " + uri);
		}
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		switch (uriMatcher.match(uri)) {
		case GROUPS_UPDATE:
			SQLiteDatabase db = mOpenHelper.getWritableDatabase();
			if(db.isOpen()) {
				int count = db.update(GROUPS_TABLE, values, selection, selectionArgs);

				getContext().getContentResolver().notifyChange(SmsUtils.GROUPS_QUERY_ALL_URI, null);
				return count;
			}
			break;
		default:
			throw new IllegalArgumentException("Unknow Uri : " + uri);
		}
		return 0;
	}

}
在清单文件中注册:

   <provider
            android:name=".provider.GroupContentProvider"
            android:authorities="com.freedom.intelligencesms.provider.GroupContentProvider" >
        </provider>

这样,一个内容提供者就写好了。当我们需要调用的时候。

String path = content://sms/
Uri uri = uri.parse(path)

ContentResolver resolver = getContentResolver();


Cursor cursor = resolver.query(uri。。。。。。。)

有时候,我们需要监听数据库中对应数据变化的时候,这时就需要用到内容观察者。也是直接上使用代码了:

//内容观察者

ContentResolver resolver = getContentResolver();

resolver.registerContentObserver(uri,true,new MyOberserver(new Handler))

private class Myobserver extends ContentObserver
{
	public MyObserver(Handler handler){
		super(handler)
	}

	//当内容观察者发现数据库变化的时候调用这个方法
	//观察到消息邮箱里面有一个数据变化时,通知
	public void onChange(boolean selfChange)
	{
		super.onChange(selfChange);
		Uri uri = uri.parse("content://sms");

		ContentResolver resolver = getContentResolver();
	

		Cursor cursor = resolver.query(uri,new String[]{"address","date",""type","body"},null,null,null);//从短信数据库获取指定内容
		while(cursor.moverToNext())
		{
			String address = cursor.getString(0)
			......

		}//获取所有短信信息

		cursor.moveToLast()//指向最后一条短信
		cursor.moveToFirst()//指向最新一条短信		


	}

}


由于准备不充分,时间冲忙,这篇文章写得不是很好,望大家谅解。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。