会话页面


Test :测试
  1. public class Test extends AndroidTestCase{
  2. public void test(){
  3. Uri uri = Uri.parse("content://sms/conversations");
  4. String[] projection={
  5. "sms.body AS snippet",
  6. "sms.thread_id AS thread_id",
  7. "groups.msg_count AS msg_count",
  8. "address as address",
  9. "date as date"
  10. };
  11. Cursor cursor = getContext().getContentResolver().query(uri, projection, null, null, " date desc");
  12. Tools.printCursor(cursor);
  13. }
  14. public void testNumber(){
  15. String number = "8888";
  16. Uri uri = PhoneLookup.CONTENT_FILTER_URI;
  17. Uri uri2 = Uri.withAppendedPath(uri, number);
  18. Cursor query = getContext().getContentResolver().query(uri2, null, null, null, null);
  19. Tools.printCursor(query);
  20. }
  21. public void testNumber2(){
  22. String number = "8888";
  23. Uri uri = PhoneLookup.CONTENT_FILTER_URI;
  24. Uri uri2 = Uri.withAppendedPath(uri, number);
  25. Cursor query = getContext().getContentResolver().query(uri, null, null, null, null);
  26. Tools.printCursor(query);
  27. }
  28. public void getFace(){
  29. // 根据号码查询联系人的头像:
  30. // Step1:查询联系人的id: URI为PhoneLookup.CONTENT_FILTER_URI
  31. // Step2:查询ContactsContract.Contacts.CONTENT_URI + 加上上面得到id, 构建好Uri之后调用ContactsContract.Contacts.openContactPhotoInputStream得到图片的流.
  32. String number ="2222";
  33. int id = Tools.findIDByNumber(getContext(), number);
  34. Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, ""+id);
  35. InputStream input = Contacts.openContactPhotoInputStream(getContext().getContentResolver(), uri);
  36. System.out.println(input);
  37. }
  38. public void findContactFromsub(){
  39. Uri uri = Phone.CONTENT_URI;
  40. // Uri uri = PhoneLookup.CONTENT_FILTER_URI;
  41. Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
  42. Tools.printCursor(cursor);
  43. }
  44. }


Tools :
  1. public class Tools {
  2. public static void printCursor(Cursor cursor) {
  3. if (cursor == null) {
  4. System.out.println("cursor == null");
  5. return;
  6. }
  7. if (cursor.getCount() == 0) {
  8. System.out.println("cursor.getCount() == 0");
  9. return;
  10. }
  11. while (cursor.moveToNext()) {
  12. int columnCount = cursor.getColumnCount();
  13. System.out.println("当前是第几行:" + cursor.getPosition());
  14. for (int i = 0; i < columnCount; i++) {
  15. String columnName = cursor.getColumnName(i);
  16. String value = cursor.getString(i);
  17. System.out.println(columnName + " : " + value);
  18. }
  19. }
  20. }
  21. /**
  22. * 通过电话号码,查询联系人姓名
  23. *
  24. * @param ctx
  25. * @param number
  26. * 要查询的电话号码
  27. * @return 返回联系人姓名,或者 null 表明联系人中,无此人
  28. */
  29. public static String findNameByNumber(Context ctx, String number) {
  30. String name = null;
  31. Uri uri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number);
  32. Cursor cursor = ctx.getContentResolver().query(uri2,
  33. new String[] { "display_name" }, null, null, null);
  34. if (cursor.getCount() == 0) {
  35. return null;
  36. } else {
  37. // cursor 返回时,默认指向的是 第一行的上一行,即 -1 行 ,而所有的数据是从 第 0行开始的。
  38. cursor.moveToNext();
  39. name = cursor.getString(0);// cursor 仅查询一列内容,所以取的时候,列的索引值为 0
  40. }
  41. return name;
  42. }
  43. /**
  44. * 通过电话号码,查询联系人ID
  45. *
  46. * @param ctx
  47. * @param number
  48. * 要查询的电话号码
  49. * @return 返回联系人ID,或者 -1 表明无此联系人中
  50. */
  51. public static int findIDByNumber(Context ctx, String number) {
  52. int contactId;
  53. Uri uri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number);
  54. Cursor cursor = ctx.getContentResolver().query(uri2,
  55. new String[] { "_id" }, null, null, null);
  56. if (cursor.getCount() == 0) {
  57. return -1;
  58. } else {
  59. // cursor 返回时,默认指向的是 第一行的上一行,即 -1 行 ,而所有的数据是从 第 0行开始的。
  60. cursor.moveToNext();
  61. contactId = cursor.getInt(0);// cursor 仅查询一列内容,所以取的时候,列的索引值为 0
  62. }
  63. return contactId;
  64. }
  65. /**
  66. * 通过联系人的ID,查询联系人的头像
  67. *
  68. * @param ctx
  69. * @param contactId
  70. * @return 返回 bitmap 头像, 如果此联系人没有头像的话,返回 null
  71. */
  72. public static Bitmap getFaceById(Context ctx, String contactId) {
  73. Bitmap bitmap = null;
  74. Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, contactId);
  75. InputStream input = Contacts.openContactPhotoInputStream(ctx.getContentResolver(), uri);
  76. if(input == null){
  77. return null;
  78. }
  79. bitmap = BitmapFactory.decodeStream(input);
  80. return bitmap;
  81. }
  82. /**
  83. * 根据会话ID,删除短信
  84. * @param ctx
  85. * @param threadId
  86. */
  87. public static void deleteMsgByThreadId(Context ctx, Integer threadId) {
  88. ctx.getContentResolver().delete(MyConstants.URI_SMS, " thread_id = ?", new String[]{""+threadId});
  89. }
  90. }
MyQueryHandler 
  1. public class MyQueryHandler extends AsyncQueryHandler{
  2. public MyQueryHandler(ContentResolver cr) {
  3. super(cr);
  4. }
  5. @Override
  6. /**
  7. * 当startQuery 执行完成后,回调 此方法
  8. * token 是startQuery 方法中的第一个参数
  9. * cookie 是startQuery 方法中的第二个参数
  10. * cursor 查询后的结果
  11. */
  12. protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
  13. System.out.println("onQueryComplete : token:"+token);
  14. System.out.println("onQueryComplete : cookie:"+cookie);
  15. Tools.printCursor(cursor);
  16. if(cookie!=null && cookie instanceof CursorAdapter){
  17. CursorAdapter adapter = (CursorAdapter) cookie;
  18. // 给adapter 设置新的cursor
  19. adapter.changeCursor(cursor);
  20. }
  21. if(cursorChangedListener!=null){
  22. cursorChangedListener.onCursorChanged(token, cookie, cursor);
  23. }
  24. }
  25. public IOnCursorChangedListener getCursorChangedListener() {
  26. return cursorChangedListener;
  27. }
  28. public void setOnCursorChangedListener(IOnCursorChangedListener cursorChangedListener) {
  29. this.cursorChangedListener = cursorChangedListener;
  30. }
  31. private IOnCursorChangedListener cursorChangedListener;
  32. /**
  33. * 声明,cursor改变时的监听接口
  34. * @author Administrator
  35. *
  36. */
  37. public interface IOnCursorChangedListener{
  38. void onCursorChanged(int token, Object cookie, Cursor cursor);
  39. }
  40. }
MyConstants 
  1. public class MyConstants {
  2. /**
  3. * 查询会话的URI
  4. */
  5. public static Uri URI_CONVERSATION = Uri.parse("content://sms/conversations");
  6. /**
  7. * 直接操作SMS表
  8. */
  9. public static Uri URI_SMS = Uri.parse("content://sms");
  10. /**
  11. * 获得所有联系人的URI
  12. */
  13. public static Uri URI_CONTACTS = Phone.CONTENT_URI;
  14. /**
  15. * 表示短信的类型 ,1 表示是接收到的短信
  16. */
  17. public static int TYPE_RECEIVE = 1;
  18. /**
  19. * 表示短信的类型 ,2 表示是发送的短信
  20. */
  21. public static int TYPE_SEND = 2;
  22. }
 

2.AsyncQueryHandler、内容提供者的更多相关文章

  1. Android开发学习—— ContentProvider内容提供者

    * 应用的数据库是不允许其他应用访问的* 内容提供者的作用就是让别的应用访问到你的数据库.把私有数据暴露给其他应用,通常,是把私有数据库的数据暴露给其他应用. Uri:包含一个具有一定格式的字符串的对 ...

  2. Android学习---通过内容提供者(ContentProvider)操作另外一个应用私有数据库的内容

    一.什么是ContentProvider? ContentProvider直译过来就是内容提供者,主要作用就是A应用提供接口给B应用调用数据,和之前介绍的sharedPreference和直接开放文件 ...

  3. Android 内容提供者的实现

    接着上文<Android 内容提供者简介>进一步实现内容提供者. 每个Content Provider类都使用URI(Universal Resource Identifier,通用资源标 ...

  4. Android 内容提供者简介

    在Android应用中,我们可以使用显式意图(Explicit Intent)来直接访问其他应用的Activity,但是这仅限于Activity的范畴:如果需要使用其他应用的数据,还需要用到另外一种组 ...

  5. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  6. 第九天 内容提供者 ContentResolver

    重点:理解ContentProvider 的作用和创建流程 1. 内容提供者,提供 其他数据库的访问. 特点       - 描述 : 它是android 四大组件之一,需要androidManife ...

  7. Android应用开发基础之九:内容提供者(ContentProvider)

    内容提供者 应用的数据库是不允许其他应用访问的 内容提供者的作用:就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代 ...

  8. 安卓第十四天笔记-内容提供者(ContentProvider)

    安卓第十四天笔记-内容提供者(ContentProvider) ContentProvider--内容提供者 1.ContentProvider简介 ContentProvider是不同应用程序之间进 ...

  9. Android组件系列----ContentProvider内容提供者

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  10. [Android Pro] 监听内容提供者ContentProvider的数据变化

    转载自:http://blog.csdn.net/woshixuye/article/details/8281385 一.提出需求 有A,B,C三个应用,B中的数据需要被共享,所以B中定义了内容提供者 ...

随机推荐

  1. Scrapy Spider MiddleWare 设置

    # -*- coding: utf-8 -*- # Define here the models for your spider middleware # # See documentation in ...

  2. 常用的三种json软件的使用

    从几个角度比较三种软件 1. json操作 2 反解 3 性能 易用性还没有列出. 可以根据个人喜好进行取舍. package json; import com.alibaba.fastjson.JS ...

  3. [转]JSOUP 抓取HTTPS/HTTP网页,校验问题

    针对一般的http请求是不需要的校验的.但是https安全校验过总过不去.最后找到以下方法,终于成功. 让我们的站点信任所有站点,不需要引包,系统自带ssl证书校验,话不多数,贴代码. /** * 信 ...

  4. PHPEXCEL读出数据是PHPExcel_RichText类型

    今天在做导入EXCEL数据时,而且单元格里的数据类型改成文本类型后,在PHPEXCEL读出来的是PHPExcel_RichText类型的,这类型使getValue()是不管用了,因为这时候getVal ...

  5. sort 对多列进行排序

    sort -t '\t' -k 3,3 -k 2,2 文件名  # 先对第三列进行排序,然后再对第二列进行排序

  6. [Git] 拉开发分支的代码报错

    Git拉开发分支的代码报错: fatal: The remote end hung up unexpectedly fatal: early EOF fatal: index-pack failed ...

  7. java - Integer、int 、String相互转换总结

    一下子还真记不清这三种数据类型之间的转换方法,所以做个小笔记. public class Test03 { public static void main(String[] args) { //int ...

  8. shell脚本学习-练习写一个脚本1

    # 1.依次展示/etc/passwd中的用户名和UID.格式如:Hello,$USER,your UID is $UID. # 2.统计一个有多少个用户 #!/bin/bash #Program D ...

  9. win10在Pycharm中安装scrapy

    查看官网说明 发现推荐是安装Anaconda 或 Miniconda,这东西有点大而全,感觉目前用不上.所以没这样做. 直接安装scrapy 如果直接装会报错的,参考文章就可以解决. 这里记一下组件下 ...

  10. Vue的双向数据绑定

    最简单的实现v-model数据绑定,只需要在一个组件里面有个props,加上一个value,然后当组件要去修改数据的时候, $emit一个input事件,并且把新的值传出去.这就实现了Vue里面的数据 ...