android 获取手机中的短信信息

获取手机中所有的Message, 并使用for循环遍历输出;

    //获取手机中所有的Message;
    private List<Map<String, Object>> getMsgInPhone() 
    {
        final String MSG_URI_ALL = "content://sms/"; //所有信息对应的message;
        List<Map<String, Object>> contentsList = new ArrayList<Map<String, Object>>();
        Uri uri = null;
        uri = Uri.parse(MSG_URI_ALL);
        //所要查询的列;
        String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, "date desc"); //获取手机内部短信;
        while(cursor.moveToNext())
        {
            Map<String, Object> map = new HashMap<String, Object>();

            String strAddress = cursor.getString(cursor.getColumnIndex("address"));//取得短信发送目标的电话;
            int intPerson = cursor.getInt(cursor.getColumnIndex("person"));  //取得索引;
            String strBody = cursor.getString(cursor.getColumnIndex("body")); //取得信息的内容;
            long longDate = cursor.getLong(cursor.getColumnIndex("date"));   //取得日期;
            int intType = cursor.getInt(cursor.getColumnIndex("type"));  //取得短信的类型,1表示已接收,2表示已发送;

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化日期;
            Date date = new Date(longDate);
            String strDate = sdf.format(date);

            map.put("listNum", strAddress);
            map.put("listMsg", strBody);
            map.put("listTime", strDate);
            if(intType == 1)
            {
                map.put("listType", "收");
            }
            else if(intType == 2)
            {
                map.put("listType", "发");
            }
            contentsList.add(map);          
        }
        //打印输出信息;
        for(int i=0; i<contentsList.size(); i++)   
        {
            Map maps = contentsList.get(i);
            Set set = maps.keySet();
            for(Object x:set)
            {
                System.out.println(x + "-----" + maps.get(x));
            }

        }
        if(!cursor.isClosed())
        {
            cursor.close();
            cursor = null;
        }
        return contentsList;
    }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值