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: -----------------------------------------------
相关推荐
蜡笔小新星26 分钟前
Flask项目框架
开发语言·前端·经验分享·后端·python·学习·flask
cliff,27 分钟前
【python爬虫】酷狗音乐爬取
笔记·爬虫·python·学习
IT猿手27 分钟前
2025最新群智能优化算法:海市蜃楼搜索优化(Mirage Search Optimization, MSO)算法求解23个经典函数测试集,MATLAB
开发语言·人工智能·算法·机器学习·matlab·机器人
IT猿手2 小时前
2025最新群智能优化算法:山羊优化算法(Goat Optimization Algorithm, GOA)求解23个经典函数测试集,MATLAB
人工智能·python·算法·数学建模·matlab·智能优化算法
萧鼎3 小时前
深入解析 Umi-OCR:高效的免费开源 OCR 文字识别工具
python·ocr·umi-ocr
*星星之火*3 小时前
【GPT入门】第5课 思维链的提出与案例
android·gpt
夏天的味道٥3 小时前
使用 Java 执行 SQL 语句和存储过程
java·开发语言·sql
EasyCVR4 小时前
EasyRTC嵌入式视频通话SDK的跨平台适配,构建web浏览器、Linux、ARM、安卓等终端的低延迟音视频通信
android·arm开发·网络协议·tcp/ip·音视频·webrtc
韩家老大4 小时前
RK Android14 在计算器内输入特定字符跳转到其他应用
android
IT、木易5 小时前
大白话JavaScript实现一个函数,将字符串中的每个单词首字母大写。
开发语言·前端·javascript·ecmascript