Android 获取所有短信-彩信

1.权限




动态申请权限:

READ_SMS

2.获取所有短信

  /**
     * 获取短信
     *
     * @return
     */
    private List> obtainPhoneMessage() {
        Uri SMS_INBOX = Uri.parse("content://sms/");
        List> list = new ArrayList<>();
        String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
//        Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc");
        Cursor cur = getContentResolver().query(SMS_INBOX, projection, null, null, null);
        if (null == cur) {
            return null;
        }
        while (cur.moveToNext()) {
            @SuppressLint("Range") String number = cur.getString(cur.getColumnIndex("address"));//手机号
            @SuppressLint("Range") String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
            @SuppressLint("Range") String body = cur.getString(cur.getColumnIndex("body"));//短信内容
            @SuppressLint("Range") String type = cur.getString(cur.getColumnIndex("type"));//短信type,1,接受,2,发送
            Map map = new HashMap();
            map.put("num", number);
            map.put("name", name);
            map.put("mess", body);
            map.put("type", type);
            list.add(map);
        }
        return list;
    }

3.获取所有彩信

/**
     * @param receive true:收到的彩信  false: 发出的彩信
     */
    @SuppressLint("Range")
    private List> getMMS(boolean receive) {
        List> list = new ArrayList<>();
        Uri parse = null;
        //这里是为了区分收件或发件,,如果不需要区分直接获取全部,可以直接使用 Uri.parse("content://mms/inbox")
        //查询所有彩信:Uri.parse("content://mms/inbox")
        if (receive) {
            //查询收件箱的彩信
            parse = Uri.parse("content://mms/inbox");
        } else {
            //查询已经发送成功的彩信
            parse = Uri.parse("content://mms/sent");
        }
        Cursor MMScursor = getContentResolver().query(parse, null, null, null, null);//查出所有彩信
        if (MMScursor == null) {
            return null;
        }
        if (!MMScursor.moveToFirst()) {
            return null;
        }
        do {
            @SuppressLint("Range") String id = MMScursor.getString(MMScursor.getColumnIndex("_id"));// 获取pdu表里 彩信的id
            String phonenumber = getAddressNumber(MMSActivity.this, id);
//            KLog.e("<<彩信>>  手机号:" + phonenumber);
            @SuppressLint("Range") int timess = MMScursor.getInt(MMScursor.getColumnIndex("date"));
            long timesslong = (long) timess * 1000;//彩信获取的时间是以秒为单位的。
//            KLog.e("<<彩信>>  时间:" + timeStampDate(timesslong));

            String selectionPart = "mid=" + id;// part表mid字段即为 pdu表 _id 字段
            //String[]        projection = new String[]{"_id", "address", "person", "body", "date", "type","protocol"};
            //从part表 获取彩信详情
            Cursor cursor = getContentResolver().query(Uri.parse("content://mms/part"), null, selectionPart, null, null); //查询 part 指定mid的数据
            if (cursor == null) {
                continue;
            } else {
                if (cursor.moveToFirst()) {
                    String body = "";//彩信文本
                    do {
                        @SuppressLint("Range") String type = cursor.getString(cursor.getColumnIndex("ct"));
                        //part表 ct字段 标识 此part内容类型,彩信始末:application/smil;如果是文本附件:text/plain;
                        //图像附件:jpg:image/jpeg,gif:image/gif;音频附件:audio/mpeg
                        if ("text/plain".equals(type)) {
                            @SuppressLint("Range") String data = cursor.getString(cursor.getColumnIndex("_data"));

                            if (data != null) {//附件地址不为空
                                @SuppressLint("Range") String partId = cursor.getString(cursor.getColumnIndex("_id"));
                                body = getMmsText(partId);
                            } else {//附件地址为空时通过text获取文本
                                //如果是彩信始末,为彩信的SMIL内容;如果是文本附件,为附件内容;如果是视频、音频附件,text为空
                                body = cursor.getString(cursor.getColumnIndex("text"));
                            }
//                            KLog.e("<<彩信>>  内容:" + body);
                        } else {
                            body = "";
                        }
                    } while (cursor.moveToNext());
                    Map map = new HashMap();
                    map.put("phone", phonenumber);
                    map.put("time", timeStampDate(timesslong));
                    map.put("content", body);
                    if (receive) {
                        map.put("type", "收到一条彩信");
                    } else {
                        map.put("type", "发送一条彩信");
                    }
                    list.add(map);
                }
            }
        } while (MMScursor.moveToNext());
        return list;
    }


    /**
     * 时间戳转换为字符串
     *
     * @param time:时间戳
     * @return
     */
    private static String timeStampDate(long time) {
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    private static String getAddressNumber(Context context, String id) {
        //此处id 也是pdu表的_id字段
        String selectionAdd = new String("msg_id=" + id);
        String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
        Uri uriAddress = Uri.parse(uriStr);
        Cursor cAdd = context.getContentResolver().query(uriAddress, null, null, null, null);
        String name = null;
        if (cAdd.moveToFirst()) {
            do {
                @SuppressLint("Range") String number = cAdd.getString(cAdd.getColumnIndex("address"));
                if (number != null) {
                    try {
                        Long.parseLong(number.replace("-", ""));
                        name = number;
                    } catch (NumberFormatException nfe) {
                        if (name == null) {
                            name = number;
                        }
                    }
                }
            } while (cAdd.moveToNext());
        }
        if (cAdd != null) {
            cAdd.close();
        }
        return name;
    }


    private String getMmsText(String id) {
        Uri partURI = Uri.parse("content://mms/part/" + id);
        InputStream is = null;
        StringBuilder sb = new StringBuilder();
        try {
            is = getContentResolver().openInputStream(partURI);
            if (is != null) {
                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                BufferedReader reader = new BufferedReader(isr);
                String temp = reader.readLine();
                while (temp != null) {
                    sb.append(temp);
                    temp = reader.readLine();
                }
            }
        } catch (IOException e) {
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return sb.toString();
    }

注意,彩信需要分开两次查询,以便区分收件与发件,如果想要一次拿取所有彩信,修改getMMS方法即可

        List> MMSList= getMMS(true);
        KLog.e("<<彩信>>  收到的彩信:" + MMSList.toString());
        List> MMSList1= getMMS(false);
        KLog.e("<<彩信>>  发出的彩信:" + MMSList1.toString());

你可能感兴趣的:(android,android,studio,java,短信,彩信)