Android 获取Sms

Android 获取Sms

本篇文章记录下android下获取短信列表.

1: 申请权限

java 复制代码
<uses-permission android:name="android.permission.READ_SMS" />

2: 获取短信内容列表

java 复制代码
    private void readSms() {
        String[] projection = {"_id", "address", "body", "type"};// 查询的字段

        String sortOrder = "date DESC"; // 按日期降序排序

        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(contentSmsUri(), projection, null, null, sortOrder);

        if (cursor != null && cursor.moveToFirst()) {
            do {
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String body = cursor.getString(cursor.getColumnIndex("body"));
                int type = cursor.getInt(cursor.getColumnIndex("type"));
                // 处理获取到的短信信息
                Log.i("xxxxx", "readSms: " + address + "|" + type + "|" + body);
            } while (cursor.moveToNext());
        }

        if (cursor != null) {
            cursor.close();
        }
    }

3: 以oppo手机为例:

java 复制代码
    private void readSms2() {
        String sortOrder = "date DESC"; // 按日期降序排序

        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(contentSmsUri(), null, null, null, sortOrder);

        if (cursor != null && cursor.moveToFirst()) {
            String[] columnNames = cursor.getColumnNames();
            do {
                for (String columnName : columnNames) {
                    int columnIndex = cursor.getColumnIndex(columnName);
                    String value = cursor.getString(columnIndex);
                    Log.d("xxxxxxx", columnName + ": " + value);
                }
                Log.d("xxxxxxx", "-----------------------------------------------");
            } while (cursor.moveToNext());
        }

        if (cursor != null) {
            cursor.close();
        }
    }

打印短信数据库cursor的所有字段:

java 复制代码
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: _id: 7
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: thread_id: 3
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: address: 1065813919
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: person: null
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: date: 1702962784973
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: date_sent: 1702962779000
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: protocol: 0
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: read: 1
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: status: -1
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: type: 1
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: reply_path_present: 0
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: subject: null
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: body: 【北京移动】尊敬的客户,您的2023年11月积分账单已送达,点击查看账单详情 https://y.10086.cn/t/f4DA6U11cVg2Y1e
      点击:https://c.139.com/m/a/s?p=hk51 领年终用户回馈福利,回Q关闭通知【中国移动 139邮箱】
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: service_center: +86xxxxxxxxxxx
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: locked: 0
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: sub_id: 1
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: phone_id: -1
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: error_code: 0
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: creator: com.android.mms
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: seen: 1
2024-03-02 12:41:35.698 17325-17325/com.test.smstest D/xxxxxxx: priority: -1
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: m_size: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_drafts: 1
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_mass: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_timer: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_groupaddress: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_collected: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_sub_date: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_service_message_sms_type: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: bubble: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: deleted: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: sync_state: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: sync_id: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_message_url: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: oppo_sms_type: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: block_type: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: favourite: 0
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_message_id: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_name: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_mime_type: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_msg_type: -1
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_msg_state: null
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_chat_type: -1
2024-03-02 12:41:35.699 17325-17325/com.test.smstest D/xxxxxxx: rcs_conversation_id: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_contribution_id: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_selector: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_transfered: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_transfer_id: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_icon: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_burn: -1
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_header: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_path: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_is_download: 0
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_size: 0
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_thumb_path: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_extend_body: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_media_played: 0
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_ext_contact: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_file_record: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_transfer_date: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_group_at_reminds: null
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: rcs_audio_read: 0
2024-03-02 12:41:35.700 17325-17325/com.test.smstest D/xxxxxxx: -----------------------------------------------
相关推荐
葡萄成熟时 !9 分钟前
黑马学生管理系统
java·开发语言
秋邱10 分钟前
高等教育 AI 智能体的 “导学诊践” 闭环
开发语言·网络·数据库·人工智能·python·docker
组合缺一17 分钟前
Solon AI 开发学习6 - chat - 两种 http 流式输入输出
python·学习·http
沐浴露z24 分钟前
为什么使用SpringAI时通常用Builder来创建对象?详解 【Builder模式】和【直接 new】的区别
java·python·建造者模式
j***630824 分钟前
MacOS升级ruby版本
开发语言·macos·ruby
青瓷程序设计39 分钟前
【宠物识别系统】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
g***789144 分钟前
鸿蒙NEXT(五):鸿蒙版React Native架构浅析
android·前端·后端
g***86691 小时前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
合作小小程序员小小店1 小时前
桌面开发,拼车管理系统开发,基于C#,winform,sql server数据库
开发语言·数据库·sql·microsoft·c#