Android 获取所有短信-彩信

2023-05-16

1.权限


<!-- 读取短信 -->
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />  

动态申请权限:


READ_SMS  

2.获取所有短信

  /**
     * 获取短信
     *
     * @return
     */
    private List<Map<String, String>> obtainPhoneMessage() {
        Uri SMS_INBOX = Uri.parse("content://sms/");
        List<Map<String, String>> 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<String, String> map = new HashMap<String, String>();
            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<Map<String, String>> getMMS(boolean receive) {
        List<Map<String, String>> 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<String, String> map = new HashMap<String, String>();
                    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<Map<String, String>> MMSList= getMMS(true);
        KLog.e("<<彩信>>  收到的彩信:" + MMSList.toString());
        List<Map<String, String>> MMSList1= getMMS(false);
        KLog.e("<<彩信>>  发出的彩信:" + MMSList1.toString());

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 获取所有短信-彩信 的相关文章

  • 低于 64K 方法时出现“方法超出编译器指令限制”消息

    我经常在日志中看到类似这样的重复消息 Method exceeds compiler instruction limit 29278 in void com xxxxxxapp xxxxxx MyGLRenderer onDrawFrame
  • Android API版本兼容性

    我希望我的应用程序能够在 Android 版本 2 1 和 2 2 上运行 在我的应用程序的一个区域中 有一个肖像式相机 生成肖像相机预览的过程在两个操作系统版本上是不同的 据我所知 具体方法如下 2 1 Camera Parameters
  • 如何在 Android 应用程序中实现 Rate It 功能

    我正在开发一个 Android 应用程序 一切正常 我的应用程序已准备好启动 但我还需要实现一项功能 我需要显示一个包含以下内容的弹出窗口 Rate It and Remind me later 在这里 如果任何用户在市场上对应用程序进行评
  • 如何将图片保存到文件中?

    我正在尝试使用标准意图来拍照 然后允许批准或重新拍摄 然后我想将图片保存到文件中 这是我正在使用的意图 Intent intent new Intent android provider MediaStore ACTION IMAGE CA
  • 在 Android Studio 中设置 Http 代理

    我已经阅读了多个类似的问题和文档 但我无法解决我的机器所在的公司防火墙的问题 我收到的错误是 无法刷新 Gradle 项目 未知主机 services gradle org 我所阅读和理解的所有内容都让我相信这是一个 http 代理问题 我
  • AlertDialog 关闭不起作用

    我有以下警报对话框 AlertDialog Builder dialogBuilder new AlertDialog Builder mContext dialogBuilder setTitle R string title dialo
  • 将 REST 服务与 Android 应用程序同步

    我使用一个 REST 服务来填充数据库中的信息 稍后由我的应用程序使用 我已经阅读了有关此事的多个主题 现在必须决定如何在 REST 服务和数据库之间实现同步 想象一个应用程序 它从谷歌金融 API 获取有关股票的信息并将其存储在数据库中
  • Android 4.3 的 Google 地图 Android API V2 问题

    我是谷歌地图的新手 刚刚点击此链接http www androidhive info 2013 08 android working with google maps v2 http www androidhive info 2013 08
  • 类型“Null”不是类型“Widget”的子类型

    我正在编写一个关于 flutter 的代码 以便在 android ios 和 web 上使用 Google 登录 但我第一时间就遇到了这个错误 我在 Android 模拟器上运行它来检查它是否正常工作 我现在还没有将其设置为网络 在模拟器
  • Android Studio 无法解析存储库

    在我的项目中 我尝试使用设计支持库 我的 Gradle 文件中有 dependencies compile com android support design 当我尝试构建这个时 我收到错误 通常我会点击Install Repositor
  • Android Fitness API 未从可穿戴传感器读取数据

    我一直在阅读有关 Google Fit API 的内容 特别是传感器 API https developers google com fit android sensors https developers google com fit a
  • 将 EditText 聚焦在设备上运行的 PopupWindow 中时出现异常

    我正在为 Android 开发一个弹出窗口 它正在工作 我在上面添加了一个 EditText 和一个按钮 当在 ADV 上运行时 它可以正常工作 而在设备上运行时 当我专注于 EditText 时 这会抛出一个奇怪的异常 android v
  • 使用远程数据编写 Android、iPad、iPhone 客户端的技术

    我需要探索世界 你写了一个杀手级应用程序 但你有 Android iPhone iPad 客户端吗 我的问题是 1 向这些设备发送数据的最佳方式是什么 按照建议进行肥皂和休息here https stackoverflow com ques
  • 文本转语音无法在 Android 设备上运行

    下面是我的代码 我无法在 Kitkat 设备中听到声音 Toast 出现 但声音没有播放 我正在遵循本教程 https www tutorialspoint com android android text to speech htm ht
  • Android Market 多个 APK...不同的 CPU 架构怎么样?

    所以我想我现在可以使用针对目标 CPU 架构的不同 NDK 编译库来上传我的应用程序 但似乎这是不可能的 有人知道如何将不同的 APK 上传到 Android Market 每个 APK 都包含专门为不同 CPU 架构编译的库吗 我还没有尝
  • 设置 JAVA_HOME 变量时出现问题

    所以我刚刚下载了 Android Studio 并尝试设置 JAVA HOME 变量以便我可以运行它 我使用的是 Windows 8 并按照我找到的所有说明进行操作 但无济于事 转到高级系统设置 gt 环境变量 然后使用包含我的 jre7
  • Xamarin.Forms 用相机拍照显示方向错误并且后退按钮崩溃

    我正在使用此处的 Xamarin Forms Camera 示例 https github com XForms Xamarin Forms Labs Samples tree master XF Labs CameraSample htt
  • 如何处理在某些 Marshmallow 之前的设备上未自动授予 SYSTEM_ALERT_WINDOW 权限

    我收到一些小米设备 例如 Mi 2 运行 API 级别 21 不显示叠加层的报告 我的应用程序以 API 23 为目标 有several http forum xda developers com xiaomi mi 3 help link
  • Android 2.2 中不带预览的相机捕获

    我需要捕获图像而不显示预览 我想在后台作为服务来完成它 可以这样做吗 是有可能实现的 您应该定义一个处理 Camera 对象的类 例如调用 Camera open 等 不要为相机对象提供以下行以禁用预览 mCamera setPreview
  • react-native-firebase 无法处理通知 click_action 导航到通知屏幕

    我正在使用这个react native firebase和react navigation进行导航 我可以成功地从服务器或控制台推送通知 无论它是在前台还是后台 但我发誓文档不太清楚如何打开通知并导航到通知它所属的屏幕 这些是我的 onMe

随机推荐