android之ContentResolver与ContentProvider介绍

2019-12-10 19:16:18于丽

      

复制代码
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, 如: 

复制代码       
content://com.shguo.statistic/sms  

 

       一般数据中都有dir和item两种(当然可定义多个)。为ContentProvider创建息的UriMatcher并添加这两者:

 

复制代码
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来操作的。 
      

复制代码
switch (sUriMatcher.match(uri))   

 

来分步操作. 
       
3. 定义MIME类型, 
      覆盖getType方法:主要是根据uri来返回Provider的MIME类型 
    

复制代码
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";  

 

 getType()为: 
    

 

复制代码
switch (sUriMatcher.match(uri)) {   
        case SMS_DIR: