1.查询手机的联系人
    public void getContacts() {ContentResolver contentResolver = this.getContentResolver();Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,null, null, null, null);if (cursor != null && cursor.moveToFirst()) {do {Log.d(TAG,cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)));Log.d(TAG,cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)));} while (cursor.moveToNext());}cursor.close();}

记得开权限

 <uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" />

log

05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 1
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 阿大
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 2
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 3
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 4
05-08 16:20:08.639 7447-7447/com.bbk.contentprovidertest D/notecontent: 海哥
cusor的正确遍历方式

查询得到的cursor是指向第一条记录之前的,因此查询得到cursor后第一次调用moveToFirst或moveToNext都可以将cursor移动到第一条记录上。但是为了规避一些可能存在的情况,下面这种写法比较保险

if (cursor != null && cursor.moveToFirst())
{do{}while(cursor.moveToNext());
}
cursor.close();

这篇文章主要讲解ContentResolver的query方法。

ContentResolver直译为内容解析器,什么东东?Android中程序间数据的共享是通过Provider/Resolver进行的。提供数据(内容)的就叫Provider,Resovler提供接口对这个内容进行解读。

根据Android文档,

public final Cursor query (Uri uri, String[] projection,String selection,String[] selectionArgs,String sortOrder)

第一个参数,uri

uri是什么呢?好吧,上面我们提到了Android提供内容的叫Provider,那么在Android中怎么区分各个Provider?有提供联系人的,有提供图片的等等。所以就需要有一个唯一的标识来标识这个Provider,Uri就是这个标识,android.provider.ContactsContract.Contacts.CONTENT_URI就是提供联系人的内容提供者,可惜这个内容提供者提供的数据很少。

第二个参数,projection,

这个参数告诉Provider要返回的内容(列Column),比如Contacts Provider提供了联系人的ID和联系人的NAME等内容,如果我们只需要NAME,那么我们就应该使用:

Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,  new String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);

当然,下面打印的你就只能显示NAME了,因为你返回的结果不包含ID。用null表示返回Provider的所有内容(列Column)。
Log

05-08 16:00:55.587 13524-13524/com.bbk.contentprovidertest D/notecontent: 阿大
05-08 16:00:55.588 13524-13524/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:00:55.588 13524-13524/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:00:55.588 13524-13524/com.bbk.contentprovidertest D/notecontent: 海哥

第三个参数,selection,

设置条件,相当于SQL语句中的where。null表示不进行筛选。如果我们只想返回名称为“海哥”的数据,第三个参数应该设置为:

 Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,new String[]{ContactsContract.Contacts.DISPLAY_NAME}, ContactsContract.Contacts.DISPLAY_NAME + "= '海哥'", null, null);//注意‘海哥’

获取到的结果,Log

05-08 16:05:16.264 20723-20723/com.bbk.contentprovidertest D/notecontent: 海哥

例如查询手机中以.txt结尾的文件

Cursor cursor = getContentResolver().query(Uri.parse("content://media/external/file"),projection,MediaStore.Files.FileColumns.DATA + " like ?" ,new String[]{"%.txt"},null);

SQL模糊查询语句

先看下面这个函数,查询手机中以.txt结尾的文件以及文档大小要超过500KB的文档。

  private void queryBooks() {String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE};//查询以.txt结尾的文件以及文件大小超过1024*500(500KB)//注意 AND  俩边空格 Cursor cursor = getContentResolver().query(Uri.parse("content://media/external/file"),projection,MediaStore.Files.FileColumns.DATA + " like ?" + " AND " + MediaStore.Files.FileColumns.SIZE + " >= ?",new String[]{"%.txt", "512000"},null);if (cursor != null && cursor.moveToFirst()) {do {int ctitle = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);int csize = cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE);Recommend.RecommendBooks books = new Recommend.RecommendBooks();long size = cursor.getLong(csize);String title = cursor.getString(ctitle);int dot = title.lastIndexOf("/");String name = title.substring(dot + 1);if (name.lastIndexOf(".") > 0) {name = name.substring(0, name.lastIndexOf("."));}books.title = name;books.lastChapter = FileUtils.formatFileSizeToString(size);books.isFromSD = true;mList.add(books);} while (cursor.moveToNext());}cursor.close();mAdapter.setListItems(mList);mAdapter.notifyDataSetChanged();mListView.setAdapter(mAdapter);}

上面这段代码直接在查询时,就明确了要求,查询效率也大幅度提升,比在查询后根据size再次判断快了200多毫秒。

SQL模糊查询,使用like比较字,加上SQL里的通配符:

like使用:

下面一些实例演示了 带有 ‘%’ 和 ‘_’ 运算符的 LIKE 子句不同的地方:

语句 描述
WHERE SALARY LIKE ‘200%’ 查找以 200 开头的任意值
WHERE SALARY LIKE ‘%200%’ 查找任意位置包含 200 的任意值
WHERE SALARY LIKE ‘_00%’ 查找第二位和第三位为 00 的任意值
WHERE SALARY LIKE ‘2_%_%’ 查找以 2 开头,且长度至少为3个字符的任意值
WHERE SALARY LIKE ‘%2’ 查找以 2 结尾的任意值
WHERE SALARY LIKE ‘_2%3’ 查找第二位为 2,且以 3 结尾的任意值
WHERE SALARY LIKE ‘2___3’ 查找长度为 5位数,且以2开头以3结尾的任意值

第四个参数,selectionArgs,

这个参数是要配合第三个参数使用的,如果你在第三个参数里面有?,那么你在selectionArgs写的数据就会替换掉?,

  Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,new String[]{ContactsContract.Contacts.DISPLAY_NAME}, ContactsContract.Contacts.DISPLAY_NAME + "= ?", new String[]{"海哥"}, null);

效果和上面一句的效果一样。

第五个参数,sortOrder,

按照什么进行排序,相当于SQL语句中的Order by。如果想要结果按照ID的降序排列:

Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,  null, null,null, android.provider.ContactsContract.Contacts._ID + " DESC");

Log

05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 海哥
05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:18:11.061 4921-4921/com.bbk.contentprovidertest D/notecontent: 阿大

升序,默认排序是升序,也可+" ASC":


Cursor cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI,  null, null,null, android.provider.ContactsContract.Contacts._ID + " ASC");

Log

05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 阿大
05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 阿牛
05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 阿虎
05-08 16:18:53.787 6316-6316/com.bbk.contentprovidertest D/notecontent: 海哥

ContentResolver.query详解相关推荐

  1. Spring Data JPA 从入门到精通~@Query详解

    语法及其源码 public @interface Query {/*** 指定JPQL的查询语句.(nativeQuery=true的时候,是原生的Sql语句)*/String value() def ...

  2. Elasticsearch 多语言及中文分词与检索详解

    文章目录 1. 自然语言与查询 Recall 2. 混合多语言的挑战 3. 分词的挑战 4. 中文分词方法的演变 - 字典法 5. 中文分词方法的演变 - 基于统计法的机器学习算法 6. 中文分词器现 ...

  3. Android之学习笔记 Contacts (一)ContentResolver query 参数详解

    1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人NAME. [java] view plaincopy public void fetchAllContacts() { ...

  4. laravel 调试mysql_Laravel - MySQL数据库的使用详解3(Query Builder用法2:新增、修改、删除)...

    五.新增.修改.删除操作 在前文中我介绍了如何使用Query Builder(查询构造器)进行数据查询,下面接着介绍如何使用它进行数据的增.删.改操作.同样假设我们有如下用户表(user): 1,新增 ...

  5. Spring Data JPA 之 @Query 语法详解及其应用

    5 Spring Data JPA 之 @Query 语法详解及其应用 5.1 快速体验 @Query 的方法 沿⽤我们之前的例⼦,新增⼀个 @Query 的⽅法: // 通过 query 注解根据 ...

  6. Vue路由传参详解(params 与 query)

    Vue路由传参详解(params 与 query) 前言: 路由传参分为 params 传参与 query 传参 params 传参类似于网络请求中的 post 请求,params 传过去的参数不会显 ...

  7. 307. Range Sum Query - Mutable | 307. 区域和检索 - 数组可修改(数据结构:线段树,图文详解)

    题目 https://leetcode.com/problems/range-sum-query-mutable/ 吐槽官方题解 这题的 英文版官方题解,配图和代码不一致,而且描述不清:力扣国内版题解 ...

  8. cursor 过滤 android,Android cursor query方法详解

    1.cursor query 方法入参 public final Cursor query (Uri uri, String[] projection,String selection,String[ ...

  9. context.getContentResolver().query()详细用法详解

    1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人NAME. [java]  view plain copy public void fetchAllContacts()  ...

最新文章

  1. 零基础Java学习之接口
  2. 95-130-410-源码-source-RabbitMQ相关-RabbitMQ Connector
  3. ubuntu中实践操作系统第二章系统调用与课件不同之处
  4. 关于AOP(面向方面的编程)
  5. 谷歌翻译插件安装使用
  6. 用MarkDown写PPT
  7. 基于FPGA的QAM调制
  8. 浅聊使用PHP实现微信公众号登录
  9. 查计算机主板,怎么查看自己电脑的主板型号是什么?主板型号查询检查方法
  10. Beaglebone Black– 智能家居控制系统 LAS - 网页服务器 Node.js 、Web Service、页面 和 TCP 请求转 UDP 发送...
  11. 上传图片报Invalid filename错误
  12. 【C/C++学习笔记】C++11 random 随机数(TR1)
  13. File类,字节字符输入输出流,缓冲流,标准流,对象序列化流
  14. C/C++使用ODBC连接MSSQL数据库
  15. 宏观经济需求与供给分析之:需求的收入效应、替代效应和需求定律
  16. 北山白云里,隐者自怡悦。
  17. yolov5 detect文件参数解释(部分)
  18. 基于stm32F4的项目总结:控制层设计(四)直流有刷电机驱动基础知识
  19. Ajax入门(附学习案例)
  20. 在线购物系统--活动图

热门文章

  1. RFM的精细化客户管理笔记总结
  2. 如何写出高质量的年终总结
  3. Linux 基金会发布免费电子书 — 《企业开源:实践》
  4. 【C++】【USACO1.1.4】项链问题
  5. Python数据分析与案例教程:分析人口普查数据-欧阳桫-专题视频课程
  6. 灵通财务软件总帐子系统需求规格书之一 (转)
  7. Python 服务器监控
  8. 使用jqprint插件实现打印页面内容
  9. 多链路聚合设备疫情检查站视频传输应用解决方案
  10. android游戏性能测试,评测 篇二十九:用游戏数据来彰显实力,华为nova5i Pro游戏性能篇...