Android学习笔记_10_ContentProvider内容提供者的使用

一、使用ContentProvider共享数据

  当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。以前我们学习过文件的操作模式,通过指定文件的操作模式为Context.MODE_WORLD_READABLE 或Context.MODE_WORLD_WRITEABLE同样可以对外共享数据,但数据的访问方式会因数据存储的方式而不同,如:采用xml文件对外共享数据,需要进行xml解析来读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。

  当应用需要通过ContentProvider对外共享数据时,第一步需要继承ContentProvider并重写下面方法:

package com.example.service;

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;

public class PersonProvider extends ContentProvider {
    private DBOpenHelper helper;
    // 不匹配返回-1
    private static final UriMatcher MATCHER = new UriMatcher(
            UriMatcher.NO_MATCH);

    private static final int PERSONS = 1;// 表示多个
    private static final int PERSON = 2; // 表示一条记录
    static {
        MATCHER.addURI("com.example.providers.personprovider", "person",
                PERSONS);
        MATCHER.addURI("com.example.providers.personprovider", "person/#",
                PERSON);
    }

    // 删除
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int num = 0;
        switch (MATCHER.match(uri)) {
        case 1:
            num = db.delete("person", selection, selectionArgs);
            break;
        case 2:
            long id = ContentUris.parseId(uri);
            String where = "id=" + id;
            if (selection != null && !"".equals(selection.trim())) {
                where += " and " + selection;
            }
            num = db.delete("person", where, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("uri参数不正确");
        }
        return num;
    }

    @Override
    public String getType(Uri uri) {
        switch (MATCHER.match(uri)) {
        case 1:
            return "vnd.android.cursor.dir/person";
        case 2:
            return "vnd.android.cursor.item/person";
        default:
            throw new IllegalArgumentException("uri参数不正确");
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = helper.getWritableDatabase();
        switch (MATCHER.match(uri)) {
        case 1:
            long rowid = db.insert("person", "name", values);
            // content://com.example.providers.personprovider/person/1
            // Uri insertUri =
            // Uri.parse("content://com.example.providers.personprovider/person/"+rowid);
            // 在原来的uri后面加上一个rowid
            Uri insertUri = ContentUris.withAppendedId(uri, rowid);
            return insertUri;

        default:
            throw new IllegalArgumentException("uri参数不正确");
        }
    }

    @Override
    public boolean onCreate() {
        helper = new DBOpenHelper(this.getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = helper.getReadableDatabase();
        switch (MATCHER.match(uri)) {
        case 1:
            return db.query("person", projection, selection, selectionArgs,
                    null, null, sortOrder);
        case 2:
            long id = ContentUris.parseId(uri);
            String where = "id=" + id;
            if (selection != null && !"".equals(selection.trim())) {
                where += " and " + selection;
            }
            return db.query("person", projection, where, selectionArgs, null,
                    null, sortOrder);
        default:
            throw new IllegalArgumentException("uri参数不正确");
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int num = 0;
        switch (MATCHER.match(uri)) {
        case 1:
            num = db.update("person", values, selection, selectionArgs);
            break;
        case 2:
            long id = ContentUris.parseId(uri);
            String where = "id=" + id;
            if (selection != null && !"".equals(selection.trim())) {
                where += " and " + selection;
            }
            num = db.update("person", values, where, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("uri参数不正确");
        }
        return num;
    }

}

 

  第二步需要在AndroidManifest.xml使用<provider>对该ContentProvider进行配置,为了能让其他应用找到该ContentProvider , ContentProvider 采用了authorities(主机名/域名)对它进行唯一标识,你可以把 ContentProvider看作是一个网站(想想,网站也是提供数据者),authorities 就是他的域名:

<manifest>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <!-- 
           配置内容提供者:
             name:PersonProvider表示内容提供者类名,包名+类。
             authorities: 内容提供者的唯一标识
         -->
        <provider android:name="com.example.service.PersonProvider" 
            android:authorities="com.example.providers.personprovider" >          
        </provider>   
   </application> </manifest>

二、在另一个应用进行测试上面的代码:

  需要用到ContentResolver和ContentValues两个类,前者封装了内容提供者的增删改查,后者实体类数据。

package com.example.test;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

public class ContentProviderTest extends AndroidTestCase{
    
    public void testInsert(){
        Uri uri=Uri.parse("content://com.example.providers.personprovider/person");
        ContentResolver resolver = this.getContext().getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "content");
        values.put("age", "500");
        resolver.insert(uri, values);
    }
    public void testUpdate(){
        Uri uri=Uri.parse("content://com.example.providers.personprovider/person/150");
        ContentResolver resolver = this.getContext().getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "provider");
        values.put("age", "50");
        resolver.update(uri, values, null, null);
    }
    public void testDelete(){
        Uri uri=Uri.parse("content://com.example.providers.personprovider/person/150");
        ContentResolver resolver = this.getContext().getContentResolver();
        resolver.delete(uri,  null, null);
    }
    public void testQuery(){
        Uri uri=Uri.parse("content://com.example.providers.personprovider/person");
        ContentResolver resolver = this.getContext().getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            Log.i("ContentProviderTest", " "+name+"  : "+age);
        }
    }
}

三、Uri介绍

  Uri代表了要操作的数据,Uri主要包含了两部分信息:1》需要操作的ContentProvider ,2》对ContentProvider中的什么数据进行操作,一个Uri由以下几部分组成:

content://com.example.providers.personprovider/person/10,其中“com.example.providers.personprovider”指主机名或authority,“person/10”表示路径。

ContentProvider(内容提供者)的scheme已经由Android所规定, scheme为:content://

主机名(或叫Authority)用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。

路径(path)可以用来表示我们要操作的数据,路径的构建应根据业务而定,如下:

要操作person表中id为10的记录,可以构建这样的路径:/person/10

要操作person表中id为10的记录的name字段, person/10/name

要操作person表中的所有记录,可以构建这样的路径:/person

要操作xxx表中的记录,可以构建这样的路径:/xxx

当然要操作的数据不一定来自数据库,也可以是文件、xml或网络等其他存储方式,如下:

要操作xml文件中person节点下的name节点,可以构建这样的路径:/person/name

如果要把一个字符串转换成Uri,可以使用Uri类中的parse()方法,如下:

Uri uri = Uri.parse("content://cn.itcast.provider.personprovider/person")

 四、监听内容提供者的数据变化:

  1、对提供内容提供者的应用加入改变通知:

 

package cn.itcast.db;

import cn.itcast.service.DBOpenHelper;
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;

public class PersonContentProvider extends ContentProvider {
    private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int PERSONS = 1;
    private static final int PERSON = 2;
    private DBOpenHelper dbOpenHelper;
    static{
        matcher.addURI("cn.itcast.providers.personprovider", "person", PERSONS);
        matcher.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);
    }

    @Override
    public boolean onCreate() {
        dbOpenHelper = new DBOpenHelper(this.getContext());
        return true;
    }
    
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int num = 0 ;//已经删除的记录数量
        switch (matcher.match(uri)) {
        case PERSONS:
            num = db.delete("person", selection, selectionArgs);
            break;            
        case PERSON:
            long id = ContentUris.parseId(uri);
            String where = "personid="+ id;
            if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
                where = where + " and "+ selection;
            }
            num = db.delete("person", where, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("Unkown Uri:"+ uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return num;
    }

    @Override
    public String getType(Uri uri) {//返回当前操作的数据类型
        switch (matcher.match(uri)) {
        case PERSONS://操作的是集合类型数据
            return "vnd.android.cursor.dir/person";
        case PERSON:
            return "vnd.android.cursor.item/person";
        default:
            throw new IllegalArgumentException("Unkown Uri:"+ uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        long id = 0 ;
        switch (matcher.match(uri)) {
        case PERSONS:
            id = db.insert("person", "personid", values);//得到记录的id
            getContext().getContentResolver().notifyChange(uri, null);
            return ContentUris.withAppendedId(uri, id);//返回代表新增记录的Uri
        case PERSON:
            id = db.insert("person", "personid", values);//得到记录的id
            String strUri = uri.toString();
            Uri personUri = Uri.parse(strUri.substring(0, strUri.lastIndexOf("/")));
            getContext().getContentResolver().notifyChange(personUri, null);
            return ContentUris.withAppendedId(personUri, id);
        default:
            throw new IllegalArgumentException("Unkown Uri:"+ uri);
        }
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        switch (matcher.match(uri)) {
        case PERSONS:
            return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
            
        case PERSON:
            long id = ContentUris.parseId(uri);
            String where = "personid="+ id;
            if(selection!=null && !"".equals(selection)){ // personid=12 and name=?
                where = where + " and "+ selection;
            }
            return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
            
        default:
            throw new IllegalArgumentException("Unkown Uri:"+ uri);
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int num = 0 ;//已经修改的记录数量
        switch (matcher.match(uri)) {
        case PERSONS:
            num = db.update("person", values, selection, selectionArgs);
            break;            
        case PERSON:
            long id = ContentUris.parseId(uri);
            String where = "personid="+ id;
            if(selection!=null && !"".equals(selection)){ 
                where = where + " and "+ selection;
            }
            num = db.update("person", values, where, selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("Unkown Uri:"+ uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);//通知数据发生变化
        return num;
    }

}
View Code

 

 

  2、注册要通知改变的uri,启动B应用:

  3、执行添加方法:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值