android系统短信库的一些用法

简介:

1、查询所有短信,按发件人进行分组

 
  1. Cursor  mCursor =  
  2.    
  3.                     managedQuery(Uri.parse("content://sms"),  
  4.    
  5.                         new String[] {"_id,address,date,read,status,type,body,count(address) as " 
  6.    
  7.                             + "totleCount from (select _id,substr(address,4) as address,date,read,status,type,body " 
  8.    
  9.                             + "from sms where address like \"+86%\" union select _id,address,date,read,status,type,body " 
  10.    
  11.                             + "from sms where address not like \"+86%\") r group by r.address order by r.date desc --"},  
  12.    
  13.                         null,  
  14.    
  15.                         null,  
  16.    
  17.                         null); 

2、删除一个联系人的所有短信会话,包括+86的号码

 
  1. /**  
  2.  
  3.  * 删除一个联系人的所有短信会话,包括+86的号码  
  4.  
  5.  * @param phone  
  6.  
  7.  */ 
  8.  
  9. public int deleteMsgSession(Context context, String phone)  
  10.  
  11. {  
  12.  
  13.     String phoneBytitle = "";  
  14.  
  15.     if (!phone.startsWith("+86"))  
  16.  
  17.     {  
  18.  
  19.         phoneBytitle = "+86" + phone;  
  20.  
  21.     }  
  22.  
  23.     else 
  24.  
  25.     {  
  26.  
  27.             phoneBytitle = phone.substring(3);  
  28.  
  29.     }  
  30.  
  31.       
  32.  
  33.     Cursor cursor =  
  34.  
  35.         context.getContentResolver()  
  36.  
  37.             .query(Uri.parse("content://sms"), new String[] {"distinct thread_id"}, "address = ? or address = ?"new String[] {phone, phoneBytitle}, null);  
  38.  
  39.     List<String> list = new ArrayList<String>();  
  40.  
  41.     if (null != cursor)  
  42.  
  43.     {  
  44.  
  45.         if (cursor.moveToFirst())  
  46.  
  47.         {  
  48.  
  49.             do 
  50.  
  51.             {  
  52.  
  53.                 int thread_id = cursor.getInt(0);  
  54.  
  55.                 list.add(String.valueOf(thread_id));  
  56.  
  57.  
  58.             } while (cursor.moveToNext());  
  59.  
  60.         }  
  61.  
  62.     }  
  63.  
  64.      if (null != cursor)  
  65.  
  66.     {  
  67.  
  68.         cursor.close();  
  69.  
  70.         cursor = null;  
  71.  
  72.     }          
  73.  
  74.     int size = list.size();  
  75.  
  76.     if(size == 0)  
  77.  
  78.     {  
  79.  
  80.         return -1;  
  81.  
  82.     }  
  83.  
  84.     else 
  85.  
  86.     {  
  87.  
  88.         int num = 0;  
  89.  
  90.         for (int i = 0; i < size; i++)  
  91.  
  92.         {  
  93.  
  94.             int res = context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + list.get(i)),  
  95.  
  96.                 nullnull);  
  97.  
  98.             num = num + res;  
  99.  
  100.         }  
  101.  
  102.           System.out.println("sms_num:" + num);  
  103.  
  104.         return num;  
  105.  
  106.     }  
  107.  

3、向系统库插入短信、版本不同插入的字段有所区别

 
  1. /**  
  2.    
  3.      * 将发送的短信保存到系统短信库中  
  4.    
  5.      */ 
  6.    
  7.     private void foreverSendMsg(String content)  
  8.    
  9.     {  
  10.    
  11.         ContentValues values = new ContentValues();  
  12.    
  13.         //系统SDK的版本号  
  14.    
  15.         String sdkVersion = android.os.Build.VERSION.SDK;  
  16.    
  17.         try 
  18.    
  19.         {  
  20.    
  21.             // 发送时间  
  22.    
  23.             values.put("date", System.currentTimeMillis());  
  24.    
  25.             // 阅读状态  
  26.    
  27.             values.put("read"1);  
  28.    
  29.             // 送达号码  
  30.    
  31.             values.put("address", phoneNumberTextView.getText().toString());  
  32.    
  33.             // 送达内容  
  34.    
  35.             values.put("body", content);  
  36.    
  37.            
  38.    
  39.             //SDK为2.1时,插入的字段  
  40.    
  41.             if(ConstValue.SDK_VERSION == Integer.valueOf(sdkVersion))  
  42.    
  43.             {  
  44.    
  45.                 values.put("status", -1);  
  46.    
  47.                 values.put("type"2);  
  48.    
  49. //                values.put("locked", 0);  
  50.    
  51.             }  
  52.    
  53.             else 
  54.    
  55.             {  
  56.    
  57.                 // 设置可见  
  58.    
  59.               values.put("seen"1);  
  60.    
  61.             }  
  62.    
  63.            
  64.    
  65.             getContentResolver().insert(Uri.parse("content://sms/sent"), values);  
  66.    
  67.         }  
  68.    
  69.         catch (Exception e)  
  70.    
  71.         {  
  72.    
  73.             e.printStackTrace();  
  74.    
  75.         }  
  76.    
  77.         finally 
  78.    
  79.         {  
  80.    
  81.             values = null;  
  82.    
  83.         } 

 





     本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817121,如需转载请自行联系原作者


相关文章
|
3天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
29天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
20 1
|
1天前
|
Android开发
Android构建系统:Android.mk(2)函数详解
Android构建系统:Android.mk(2)函数详解
10 1
|
1天前
|
存储 Java API
Android系统 文件访问权限笔记
Android系统 文件访问权限笔记
24 1
|
1天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
15 1
|
1天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
13 0
|
1天前
|
测试技术 Android开发 开发者
RK3568 Android系统客制化动态替换ro任意属性
RK3568 Android系统客制化动态替换ro任意属性
11 1
|
1天前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
14 0
|
1天前
|
存储 缓存 安全
Android系统 应用存储路径与权限
Android系统 应用存储路径与权限
5 0
Android系统 应用存储路径与权限
|
1天前
|
存储 开发工具 Android开发
Android系统 权限组管理和兼容性
Android系统 权限组管理和兼容性
8 0