首页 > 代码库 > Android组件之自定义ContentProvider

Android组件之自定义ContentProvider

Android的数据存储有五种方式Shared Preferences、网络存储、文件存储、外储存储、SQLite,一般这些存储都只是在单独的一个应用程序之中达到一个数据的共享,有时候我们需要操作其他应用程序的一些数据,例如常见系统里的通讯录,短信,照片等等,所以云存储,通讯录,艳照门等等就孕育而生了。ContentProvider可以理解成内容提供者,也可以理解为一个接口,就是提供了一个供外部访问的接口,有的时候需要进行权限控制。

ContentProvider简介

ContentProvider向我们提供了我们在应用程序之前共享数据的一种机制,而我们知道每一个应用程序都是运行在不同的应用程序的,不同程序的之间数据共享是现实的需要,程序总不能使闭环的,Android中的ContentProvider外共享数据的好处是统一了数据的访问方式。简单总结说下:

  1. ContentProvider为存储和获取数据提供了统一的接口。ContentProvide对数据进行封装,不用关心数据存储的细节。使用表的形式来组织数据。
  2. 使用ContentProvider可以在不同的应用程序之间共享数据。 
  3. Android为常见的一些数据提供了默认的ContentProvider(包括音频、视频、图片和通讯录等)。 

说到了ContentProvider这么多好处,不能不说下Uri(Universal Resource Identifier)注意不是URL,通用资源标识符,看个简单的读取联系人的Uri,content://contacts/people,

  1. content://是前缀,固定的;
  2. contacts 主机名(或叫Authority)用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来调用;
  3. people  路径(path)表示我们要操作的数据,路径的构建根据业务而定;

自定义ContentProvider

俗话说,欲善其事必先利其器,想要成为一个内容提供者,就先需要有数据,先建立一个SqlDbConncetion:

public class SqlDBConnection extends SQLiteOpenHelper {	private static final String DbName ="Book.db";	private static int version=1;	public SqlDBConnection(Context context) {		super(context, DbName, null, version);	}	@Override	public void onCreate(SQLiteDatabase db) {		// TODO Auto-generated method stub		 String sqlString="create table Book (id integer primary key autoincrement,Name nvarchar(200),Title nvarchar(200))";		 db.execSQL(sqlString);	}	@Override	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {		// TODO Auto-generated method stub	}}

  上篇文章讲的junit测试这个时候可以拿过来使用一下初始化下数据:

public class BookCase extends AndroidTestCase {	public void Intial() {		SqlDBConnection dbConnection = new SqlDBConnection(getContext());		SQLiteDatabase sqlDataBase = dbConnection.getWritableDatabase();		long row = 0;		for (int i = 0; i < 5; i++) {			ContentValues values = new ContentValues();			values.put("Name", "书籍"+i);			values.put("Title", "标题" + i);			row = sqlDataBase.insert("Book", null, values);			Log.i("BookCase", "插入成功:" + row);		}	}}

 前面是基础工作,这个时候就可以建立一个自己的ContentProvider:

public class MyContentProvider extends ContentProvider {	private static final String AUTHORITY = "com.example.googlecontentprovider.MyContentProvider";	private static final int CONTENT_INSERT = 0;	private static final int CONTENT_QUERY = 1;	private static final int CONTENT_DELETE = 2;	private static final int CONTENT_UPDATE = 3;	private static final int CONTENT_QUERY_SINGLE = 4;	private static UriMatcher uriMatcher;	private SqlDBConnection dbConnection = null;		static {		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);		uriMatcher.addURI(AUTHORITY, "book/insert", CONTENT_INSERT);		uriMatcher.addURI(AUTHORITY, "book/query", CONTENT_QUERY);		uriMatcher.addURI(AUTHORITY, "book/delete", CONTENT_DELETE);		uriMatcher.addURI(AUTHORITY, "book/update", CONTENT_UPDATE);		uriMatcher.addURI(AUTHORITY, "book/query/#", CONTENT_QUERY_SINGLE);	}	@Override	public boolean onCreate() {		dbConnection = new SqlDBConnection(getContext());				return true;	}	@Override	public Cursor query(Uri uri, String[] projection, String selection,			String[] selectionArgs, String sortOrder) {		SQLiteDatabase dbDatabase = dbConnection.getWritableDatabase();		switch (uriMatcher.match(uri)) {		case CONTENT_QUERY:			if (dbDatabase.isOpen()) {				Cursor cursor=dbDatabase.query("Book", projection, selection, selectionArgs, null, null, null);				return cursor;			}			break;		case CONTENT_QUERY_SINGLE:			if (dbDatabase.isOpen()) {				long id=ContentUris.parseId(uri);				Cursor cursor=dbDatabase.query("Book", projection, "id=?", new String[]{id+""}, null, null, null);				return cursor;			}			break;		default:			break;		}		return null;	}	@Override	public String getType(Uri uri) {		// TODO Auto-generated method stub		return null;	}	@Override	public Uri insert(Uri uri, ContentValues values) {		SQLiteDatabase dbDatabase = dbConnection.getWritableDatabase();		switch (uriMatcher.match(uri)) {		case CONTENT_INSERT:			if (dbDatabase.isOpen()) {				long id = dbDatabase.insert("Book", null, values);				dbDatabase.close();				return ContentUris.withAppendedId(uri, id);			}			break;		default:			break;		}		return null;	}	@Override	public int delete(Uri uri, String selection, String[] selectionArgs) {		// TODO Auto-generated method stub		SQLiteDatabase dbDatabase = dbConnection.getWritableDatabase();		switch (uriMatcher.match(uri)) {		case CONTENT_DELETE:			if (dbDatabase.isOpen()) {				int count= dbDatabase.delete("Book", selection, selectionArgs);				dbDatabase.close();				return count;			}			break;		default:			break;		}		return 0;	}	@Override	public int update(Uri uri, ContentValues values, String selection,			String[] selectionArgs) {		SQLiteDatabase dbDatabase = dbConnection.getWritableDatabase();		switch (uriMatcher.match(uri)) {		case CONTENT_UPDATE:			if (dbDatabase.isOpen()) {				int count= dbDatabase.update("Book", values, selection, selectionArgs);				dbDatabase.close();				return count;			}			break;		default:			break;		}		return 0;	}}

 主机名是需要自己去AndroidManifest.xml文件中自己配置的,要求是唯一的,最好是用包名就好:

   <provider android:name="com.example.googlecontentprovider.MyContentProvider"            android:authorities="com.example.googlecontentprovider.MyContentProvider"></provider>

  如果觉得上面的那一串代码不是很好理解,下面调用的时候我会分别解释。

ContentResolver的使用

方法写在一个应用程序中调用属于正常,在另外一个程序中调用该程序的方法就是类似于接口了,下面先看原来初始化的数据:

重新新建一个Android测试项目,定义为BookCase,首先插入数据,定义一个Uri,这里面主机名就是上面定义的包名,book/insert与CONTENT_INSERT是对应的:

	public void bookInsert() {		Uri uri = Uri				.parse("content://com.example.googlecontentprovider.MyContentProvider/book/insert");		ContentResolver resolver = getContext().getContentResolver();		ContentValues values = new ContentValues();		values.put("Name", "书籍5");		values.put("Title", "标题5");		uri = resolver.insert(uri, values);		Log.i("BookCase", "Uri" + uri);		long id = ContentUris.parseId(uri);		Log.i("BookCase", "测试成功" + id);	}

  显示结果如下:

然后更新刚才插入的数据,同样的更具Code给Uri赋值,然后初始化一个ContentResolver,调用update方法:

	public void bookUpdate() {		Uri uri = Uri				.parse("content://com.example.googlecontentprovider.MyContentProvider/book/update");		ContentResolver resolver = getContext().getContentResolver();		ContentValues values=new ContentValues();		values.put("Name", "修改");		int count = resolver.update(uri, values, " id=?",new String[]{"10"});		Log.i("BookCase", "更新了" + count + "行");	}

  结果如下:

删除插入的数据:

public void bookDelete() {		Uri uri = Uri				.parse("content://com.example.googlecontentprovider.MyContentProvider/book/delete");		ContentResolver resolver = getContext().getContentResolver();		String where =" id=?";		String[] argString = {"10"};		int count = resolver.delete(uri, where, argString);		Log.i("BookCase", "删除了" + count + "行");	}

结果如下:

查询所有的数据:

	public void bookQuery() {		Uri uri = Uri				.parse("content://com.example.googlecontentprovider.MyContentProvider/book/query");		ContentResolver resolver = getContext().getContentResolver();		Cursor  cursor=resolver.query(uri, new String[]{"id","Name","Title"}, null, null, null);		if (cursor.getCount()>0) {			while (cursor.moveToNext()) {				int id=cursor.getInt(cursor.getColumnIndex("Id"));				String nameString=cursor.getString(cursor.getColumnIndex("Name"));				String titleString=cursor.getString(cursor.getColumnIndex("Title"));				Log.i("BookCase", id+"---"+nameString+"---"+titleString);			}		}			}

  Log下的记录:

查询单条记录:

	public void bookQuerySingle() {		Uri uri = Uri				.parse("content://com.example.googlecontentprovider.MyContentProvider/book/query");		ContentResolver resolver = getContext().getContentResolver();		uri=ContentUris.withAppendedId(uri,1);		Cursor  cursor=resolver.query(uri, new String[]{"id","Name","Title"}, null, null, null);		if (cursor.getCount()>0) {			while (cursor.moveToNext()) {				int id=cursor.getInt(cursor.getColumnIndex("Id"));				String nameString=cursor.getString(cursor.getColumnIndex("Name"));				String titleString=cursor.getString(cursor.getColumnIndex("Title"));				Log.i("BookCase", id+"---"+nameString+"---"+titleString);			}		}	}

 结果如图:

至此一个自定义ContentProvider全部搞定,如有不当,请各位多多指教~

Android组件之自定义ContentProvider