系统短信库的一些用法

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

复制代码
Cursor  mCursor =
                    managedQuery(Uri.parse("content://sms"),
                        new String[] {"_id,address,date,read,status,type,body,count(address) as "
                            + "totleCount from (select _id,substr(address,4) as address,date,read,status,type,body "
                            + "from sms where address like \"+86%\" union select _id,address,date,read,status,type,body "
                            + "from sms where address not like \"+86%\") r group by r.address order by r.date desc --"},
                        null,
                        null,
                        null);
复制代码

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

复制代码
    /**
     * 删除一个联系人的所有短信会话,包括+86的号码
     * @param phone
     */
    public int deleteMsgSession(Context context, String phone)
    {
        String phoneBytitle = "";
        if (!phone.startsWith("+86"))
        {
            phoneBytitle = "+86" + phone;
        }
        else
        {
                phoneBytitle = phone.substring(3);
        }
        
        Cursor cursor =
            context.getContentResolver()
                .query(Uri.parse("content://sms"), new String[] {"distinct thread_id"}, "address = ? or address = ?", new String[] {phone, phoneBytitle}, null);
        List<String> list = new ArrayList<String>();
        if (null != cursor)
        {
            if (cursor.moveToFirst())
            {
                do
                {
                    int thread_id = cursor.getInt(0);
                    list.add(String.valueOf(thread_id));

                } while (cursor.moveToNext());
            }
        }
         if (null != cursor)
        {
            cursor.close();
            cursor = null;
        }        
        int size = list.size();
        if(size == 0)
        {
            return -1;
        }
        else
        {
            int num = 0;
            for (int i = 0; i < size; i++)
            {
                int res = context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + list.get(i)),
                    null, null);
                num = num + res;
            }
//            System.out.println("sms_num:" + num);
            return num;
        }
    }
复制代码

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

 

复制代码
 /**
     * 将发送的短信保存到系统短信库中
     */
    private void foreverSendMsg(String content)
    {
        ContentValues values = new ContentValues();
        //系统SDK的版本号
        String sdkVersion = android.os.Build.VERSION.SDK;
        try
        {
            // 发送时间
            values.put("date", System.currentTimeMillis());
            // 阅读状态
            values.put("read", 1);
            // 送达号码
            values.put("address", phoneNumberTextView.getText().toString());
            // 送达内容
            values.put("body", content);
         
            //SDK为2.1时,插入的字段
            if(ConstValue.SDK_VERSION == Integer.valueOf(sdkVersion))
            {
                values.put("status", -1);
                values.put("type", 2);
//                values.put("locked", 0);
            }
            else
            {
                // 设置可见
              values.put("seen", 1);
            }
         
            getContentResolver().insert(Uri.parse("content://sms/sent"), values);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            values = null;
        }
复制代码

 

 

posted on   vus520  阅读(316)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2011年3月 >
27 28 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示