---
title: "android之ContentResolver与ContentProvider "
author: "javacode23"
date: "2010-11-24T23:24:55Z"
tags: []
categories: ["移动开发"]
slug: "android-contentresolver-contentprovider"
draft: false
lastmod: "2010-11-24T23:24:55Z"
id: 822007
url: "https://javacode23.iteye.com/blog/822007"
---

android中对数据操作包含有： 
file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据，程序不能通过这三种方法去操作别的应用内的数据。 
android中提供ContectResolver与ContentProvider来操作别的应用程序的数据。 

一、 使用方式 
一个应用实现ContentProvider来提供内容给别的应用来操作， 
一个应用通过ContentResolver来操作别的应用数据，当然在自己的应用中也可以。 
1. ContentResolver的获取 
   通过Context类： 
   
Java代码
public abstract ContentResolver getContentResolver();   
public abstract ContentResolver getContentResolver();
   
2. ContentResolver常用操作 
   
Java代码
//查询：    
public final Cursor query(Uri uri, String projection,    
           String selection, String selectionArgs, String sortOrder);    
//新增    
public final Uri insert(Uri url, ContentValues values)        
//更新    
public final int update(Uri uri, ContentValues values, String where,    
             String selectionArgs)    
//删除    
public final int delete(Uri url, String where, String selectionArgs)    
          
//查询：
public final Cursor query(Uri uri, String projection,
          String selection, String selectionArgs, String sortOrder);
//新增
public final Uri insert(Uri url, ContentValues values)    
//更新
 public final int update(Uri uri, ContentValues values, String where,
            String selectionArgs)
//删除
public final int delete(Uri url, String where, String selectionArgs)
       以上操作实际是通过Uri来匹配ContentProvider， 再由ContentProvider来进行具体操作的。 
       操作的参数和操作sqlite各函数的参数意义是一样的。 
       
二、实现ContentProvider提供给外界访问 
调用者ContentResoler是通过一个Uri来找到相应的ContentProvider的来进行实际操作。 
     1. Uri概念 
        一个Uri的样子如： 
      
Java代码
scheme://authorities/path/id   
scheme://authorities/path/id
       如电话记录： 
       
Java代码
public static final Uri CONTENT_URI = Uri.parse("content://call_log/calls");   
public static final Uri CONTENT_URI = Uri.parse("content://call_log/calls");
       a.根据scheme不同调用不程序来处理, 常用的：content, android_resource, file, http等 
       b.authorities是provider定义的，在AndroidManifest.xml中定义 
       c.path和id就好理解的。 
     
     2. Uri定义 
       创建自己的Uri, 如： 
       
Java代码
content://com.shguo.statistic/sms   
content://com.shguo.statistic/sms
       一般数据中都有dir和item两种(当然可定义多个)。为ContentProvider创建息的UriMatcher并添加这两者： 
       
Java代码
String AUTHORITY = "com.shguo.statistics";    
UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);    
sUriMatcher.addURI(AUTHORITY, "sms",   SMS_DIR);   //SMS_DIR = 1    
sUriMatcher.addURI(AUTHORITY, "sms/#", SMS_ITEM); //SMS_ITEM = 2   
String AUTHORITY = "com.shguo.statistics";
UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(AUTHORITY, "sms",  SMS_DIR);   //SMS_DIR = 1
sUriMatcher.addURI(AUTHORITY, "sms/#", SMS_ITEM); //SMS_ITEM = 2
       contentProvider要根据传入uri判断是dir还是item来操作的。 
       
Java代码
switch (sUriMatcher.match(uri))    
switch (sUriMatcher.match(uri))
       来分步操作. 
       
     3. 定义MIME类型， 
      覆盖getType方法:主要是根据uri来返回Provider的MIME类型 
     
Java代码
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.shguo.sms";    
ublic static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.shguo.sms";   
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.shguo.sms";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.shguo.sms";
      getType()为： 
      
Java代码
switch (sUriMatcher.match(uri)) {    
        case SMS_DIR:    
            return   CONTENT_TYPE;    
        case SMS_ITEM:    
            return CONTENT_ITEM_TYPE;    
        default:    
            throw new IllegalArgumentException("Unknown URI " + uri);    
     }   
switch (sUriMatcher.match(uri)) {
   case SMS_DIR:
    return  CONTENT_TYPE;
   case SMS_ITEM:
    return CONTENT_ITEM_TYPE;
   default:
    throw new IllegalArgumentException("Unknown URI " + uri);
  }
     
     4. 实现query, insert, delete, update四个操作。 
       具体的实现可以用sqlite, file等。并根据uri分情况操作。 
       a. query时如果是item加查询条件id. 
          where = "_ID=" + uri.getPathSegments().get(1)   + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""; 
          最后要加上 
         cursor.setNotificationUri(getContext().getContentResolver(), uri); 
        
       b. insert时要求uri只能是dir. 成功之后返回一个加id的uri. 
          
Java代码
Uri insertUri = ContentUris.withAppendedId(CONTENT_URI, rowId);   
Uri insertUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
       c. update、delete与query差不多。 
          
Java代码
//注意通知注册uri的观察者。    
getContext().getContentResolver().notifyChange(uri, null);   
//注意通知注册uri的观察者。
getContext().getContentResolver().notifyChange(uri, null);
                  
     5. 在AndroidManifest.xml中定义 
        provider元素，主要属性有： 
       
Java代码
name => ContentProvider类名    
authorities => content type的授权部分    
multiprocess => true允许在每个客户进程中创建provider实例，消除执行IPC的需求。