在上篇博客中我们介绍了自定义ContentProvider,但是遗漏掉了一个方法,那就是getType,自定义ContentProvider一般用不上getType方法,但我们还是一起来探究下这个方法究竟是干什么的?

我们先来看看ContentProvider中对这个类的定义:

    /**
* Implement this to handle requests for the MIME type of the data at the
* given URI. The returned MIME type should start with
* <code>vnd.android.cursor.item</code> for a single record,
* or <code>vnd.android.cursor.dir/</code> for multiple items.
* This method can be called from multiple threads, as described in
* <a href="{@docRoot}guide/topics/fundamentals/processes-and-threads.html#Threads">Processes
* and Threads</a>.
*
* <p>Note that there are no permissions needed for an application to
* access this information; if your content provider requires read and/or
* write permissions, or is not exported, all applications can still call
* this method regardless of their access permissions. This allows them
* to retrieve the MIME type for a URI when dispatching intents.
*
* @param uri the URI to query.
* @return a MIME type string, or {@code null} if there is no type.
*/
public abstract @Nullable String getType(@NonNull Uri uri);

注释说的也算是比较清楚了,根据给定的Uri返回一个MIME类型的数据,如果是单条数据,那么我们的MIME类型应该以vnd.android.cursor.item开头,如果是多条数据,我们的MIME类型的数据应该以vnd.android.cursor.dir开头,同时,注释还很明确的告诉我们,对于没有访问该ContentProvider权限的应用依然可以调用它的getType方法。

那么我们先来看看什么是MIME,根据维基百科上的解释,MIME是多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions)是一个互联网标准,这话太笼统,大家可以 看看w3c上的解释http://www.w3school.com.cn/media/media_mimeref.asp,这里有详细的举例。

参考网上的信息,getType的作用应该是这样的,以指定的两种方式开头,android可以顺利识别出这是单条数据还是多条数据,比如在上篇博客中,我们的查询结果是一个Cursor,我们可以根据getType方法中传进来的Uri判断出query方法要返回的Cursor中只有一条数据还是有多条数据,这个有什么用呢?如果我们在getType方法中返回一个null或者是返回一个自定义的android不能识别的MIME类型,那么当我们在query方法中返回Cursor的时候,系统要对Cursor进行分析,进而得出结论,知道该Cursor只有一条数据还是有多条数据,但是如果我们按照Google的建议,手动的返回了相应的MIME,那么系统就不会自己去分析了,这样可以提高一丢点的系统性能。基于此,我们上篇自定义的ContentProvider中的getType方法可以这么写:

	@Override
public String getType(Uri uri) {
int code = matcher.match(uri);
switch (code) {
case 1:
// 查询多条数据
return "vnd.android.cursor.dir/multi";
case 2:
case 3:
// 根据id或者姓名查询一条数据
return "vnd.android.cursor.item/single";
}
return null;
}

MIME前面的一部分我们按照Google的要求来写,后面一部分就可以根据我们自己的实际需要来写。

还有一种我们可能会很少遇到的情况,我们有可能不知道ContentProvider返回给我们的是什么,这个时候我们可以先调用ContentProvider的getType,根据getType的不同返回值做相应的处理。

就这些,欢迎拍砖指正。

最新文章

  1. BZOJ 3289: Mato的文件管理
  2. Web 播放声音(AMR 、WAVE)
  3. C/C++程序员必须熟练应用的开源项目
  4. HDOJ 3652 B-number
  5. C#借助谷歌翻译实现翻译小工具(二)添加托盘图标
  6. powerdesigner12.5 设置表字符集和存储引擎
  7. .NET验证码控件(美观 易用)
  8. su: cannot set user id: Resource temporarily unavailable
  9. 从麦肯锡到小黑裙-Project Gravitas |华丽志
  10. android程序报错“error launching activity com.android.ddmlib.shellcommandunresponsiveexception”的解决方式
  11. Docker aufs存储驱动layer、diff、mnt目录的区别
  12. win10安装sqlserver2016准备
  13. 【javascript】回调函数
  14. 解决WebUploader在谷歌浏览器中反应缓慢迟钝
  15. [对smartMenu.js改进] 解决右键菜单栏在边缘弹出后,移出视图区域无法操作的问题
  16. yum仓库搭建
  17. 洛谷 P1359 租用游艇【dp】(经典)
  18. Matplotlib新手上路(下)
  19. css案例 - mask遮罩层的华丽写法
  20. LIBS+=

热门文章

  1. Form.KeyPreview 属性
  2. JavaScript功能一览
  3. Linux kernel 拒绝服务漏洞
  4. [FOJ 1752] A^B mod C
  5. weblogic配置数据源出错
  6. 使用Action、Func和Lambda表达式
  7. 调试 Azure 云服务项目的方法
  8. CosCos2D-android 代码总结
  9. 算法导论学习-RED-BLACK TREE
  10. A Tour of Go Multiple results