Android 获取彩信文本内容及 发送时间 发送人

    xiaoxiao2023-10-25  162

    final String SMS_URI_MMS = "content://mms"; //此处查询的表是pdu表 ContentResolver MMScr = context.getContentResolver(); Uri MMSuri = Uri.parse(SMS_URI_MMS); Cursor MMScursor = null; MMScursor = MMScr.query(MMSuri, null, null, null, null);//查出所有彩信 if (MMScursor == null){ return; } if (!MMScursor.moveToFirst()){//跳转到第一行,如果失败跳出方法,成功则进入do while循环 return; } do { JSONObject jsonObject=new JSONObject(); String id=MMScursor.getString(MMScursor.getColumnIndex("_id"));// 获取pdu表里 彩信的id String phonenumber=getAddressNumber(context,id); int timess=MMScursor.getInt(MMScursor.getColumnIndex("date") ); long timesslong=(long) timess*1000;//彩信获取的时间是以秒为单位的。 Date d = new Date(timesslong); String date = dateFormat.format(d); String sub=MMScursor.getString(MMScursor.getColumnIndex("sub"));//获取彩信主题 try { if (!TextUtil.isEmpty(sub)){ sub=new String(sub.getBytes("ISO8859_1"), "UTF-8"); //测试时发现乱码https://www.xuebuyuan.com/2198269.html 从该博文找到的解决方法 }else { sub=" "; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } final String SMS_URI_MMS_PART="content://mms/part"; //从part表 获取彩信详情 String selectionPart = "mid=" + id;// part表mid字段即为 pdu表 _id 字段 ContentResolver Scr = context.getContentResolver(); //String[] projection = new String[]{"_id", "address", "person", "body", "date", "type","protocol"}; Uri uri = Uri.parse(SMS_URI_MMS_PART); Cursor cursor = Scr.query(uri, null,selectionPart, null, null); //查询 part 指定mid的数据 if (cursor==null){ continue; }else { if (cursor.moveToFirst()){//游标移至第一行成功 则进入do while 循环 一个mid查询出来的结果可能是多条,彩信的结构也   //是多层的 有点类似html的结构 do { 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)) { String data = cursor.getString(cursor.getColumnIndex("_data"));   // 当此part类型为文本附件时,通过_data拿到文本附件地址 String body;//彩信文本 if (data != null) {//附件地址不为空 // implementation of this method below String partId = cursor.getString(cursor.getColumnIndex("_id")); body = getMmsText(context,partId); try { jsonObject.putOpt("content", EmojiParser.removeAllEmojis(sub+body));   //因为要存数据库 删掉所有emoji } catch (JSONException e) { e.printStackTrace(); } } else {//附件地址为空时通过text获取文本 //如果是彩信始末,为彩信的SMIL内容;如果是文本附件,为附件内容;如果是视频、音频附件,text为空 body = cursor.getString(cursor.getColumnIndex("text")); try { jsonObject.putOpt("content", EmojiParser.removeAllEmojis(sub+body)); } catch (JSONException e) { e.printStackTrace(); } } } }while (cursor.moveToNext()); } } Logger.w("duanxin","彩信="+jsonObject.toString() ); jsonArray.put(jsonObject); }while (MMScursor.moveToNext());

     

    private static String getMmsText(Context context, String id) {   //此处id 为part表的_id 字段 Uri partURI = Uri.parse("content://mms/part/" + id); InputStream is = null; StringBuilder sb = new StringBuilder(); try { try { is = context.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 (FileNotFoundException e) { e.printStackTrace(); } } catch (IOException e) {} finally { if (is != null) { try { is.close(); } catch (IOException e) {} } } return sb.toString(); } 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 { 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; }

    https://stackoverflow.com/questions/3012287/how-to-read-mms-data-in-android?answertab=active#tab-top

    本文大部分都是摘抄的国外大神的

    最新回复(0)