Android 组件ContentProvider

来源:互联网 发布:手机淘宝下载地址 编辑:程序博客网 时间:2024/05/17 22:36

Android 组件ContentProvider

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

ContentProvider简介

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

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

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

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

自定义ContentProvider

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

view sourceprint?
01.publicclassSqlDBConnectionextendsSQLiteOpenHelper {
02. 
03.privatestaticfinalString DbName ="Book.db";
04.privatestaticintversion=1;
05. 
06.publicSqlDBConnection(Context context) {
07.super(context, DbName,null, version);
08.}
09. 
10.@Override
11.publicvoidonCreate(SQLiteDatabase db) {
12.// TODO Auto-generated method stub
13.String sqlString="create table Book (id integer primary key autoincrement,Name nvarchar(200),Title nvarchar(200))";
14.db.execSQL(sqlString);
15.}
16. 
17.@Override
18.publicvoidonUpgrade(SQLiteDatabase db,intoldVersion,intnewVersion) {
19.// TODO Auto-generated method stub
20.}
21.}

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

view sourceprint?
01.publicclassBookCaseextendsAndroidTestCase {
02. 
03.publicvoidIntial() {
04.SqlDBConnection dbConnection =newSqlDBConnection(getContext());
05.SQLiteDatabase sqlDataBase = dbConnection.getWritableDatabase();
06.longrow =0;
07.for(inti =0; i <5; i++) {
08.ContentValues values =newContentValues();
09.values.put("Name","书籍"+i);
10.values.put("Title","标题"+ i);
11.row = sqlDataBase.insert("Book",null, values);
12.Log.i("BookCase","插入成功:"+ row);
13.}
14.}
15.}

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

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

view sourceprint?
1.<provider android:name="com.example.googlecontentprovider.MyContentProvider"
2.android:authorities="com.example.googlecontentprovider.MyContentProvider"></provider>

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

ContentResolver的使用

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

\

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

view sourceprint?
01.publicvoidbookInsert() {
02.Uri uri = Uri
03..parse("content://com.example.googlecontentprovider.MyContentProvider/book/insert");
04.ContentResolver resolver = getContext().getContentResolver();
05.ContentValues values =newContentValues();
06.values.put("Name","书籍5");
07.values.put("Title","标题5");
08.uri = resolver.insert(uri, values);
09.Log.i("BookCase","Uri"+ uri);
10.longid = ContentUris.parseId(uri);
11.Log.i("BookCase","测试成功"+ id);
12.}

  显示结果如下:

\

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

view sourceprint?
01.publicvoidbookUpdate() {
02.Uri uri = Uri
03..parse("content://com.example.googlecontentprovider.MyContentProvider/book/update");
04.ContentResolver resolver = getContext().getContentResolver();
05.ContentValues values=newContentValues();
06.values.put("Name","修改");
07.intcount = resolver.update(uri, values," id=?",newString[]{"10"});
08.Log.i("BookCase","更新了"+ count + "行");
09.}

  结果如下:

\

删除插入的数据:

view sourceprint?
01.publicvoidbookDelete() {
02.Uri uri = Uri
03..parse("content://com.example.googlecontentprovider.MyContentProvider/book/delete");
04.ContentResolver resolver = getContext().getContentResolver();
05.String where =" id=?";
06.String[] argString = {"10"};
07.intcount = resolver.delete(uri, where, argString);
08.Log.i("BookCase","删除了"+ count + "行");
09.}

结果如下:

\

查询所有的数据:

view sourceprint?
01.publicvoidbookQuery() {
02.Uri uri = Uri
03..parse("content://com.example.googlecontentprovider.MyContentProvider/book/query");
04.ContentResolver resolver = getContext().getContentResolver();
05.Cursor  cursor=resolver.query(uri,newString[]{"id","Name","Title"},null,null,null);
06.if(cursor.getCount()>0) {
07.while(cursor.moveToNext()) {
08.intid=cursor.getInt(cursor.getColumnIndex("Id"));
09.String nameString=cursor.getString(cursor.getColumnIndex("Name"));
10.String titleString=cursor.getString(cursor.getColumnIndex("Title"));
11.Log.i("BookCase", id+"---"+nameString+"---"+titleString);
12.}
13.}
14. 
15.}

  Log下的记录:

\

查询单条记录:

view sourceprint?
01.publicvoidbookQuerySingle() {
02.Uri uri = Uri
03..parse("content://com.example.googlecontentprovider.MyContentProvider/book/query");
04.ContentResolver resolver = getContext().getContentResolver();
05.uri=ContentUris.withAppendedId(uri,1);
06.Cursor  cursor=resolver.query(uri,newString[]{"id","Name","Title"},null,null,null);
07.if(cursor.getCount()>0) {
08.while(cursor.moveToNext()) {
09.intid=cursor.getInt(cursor.getColumnIndex("Id"));
10.String nameString=cursor.getString(cursor.getColumnIndex("Name"));
11.String titleString=cursor.getString(cursor.getColumnIndex("Title"));
12.Log.i("BookCase", id+"---"+nameString+"---"+titleString);
13.}
14.}
15.}

 结果如图:

0 0
原创粉丝点击