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: -----------------------------------------------
相关推荐
m0_480502648 分钟前
Rust 登堂 之 函数式编程(三)
开发语言·后端·rust
辣椒http_出海辣椒25 分钟前
如何使用python 抓取Google搜索数据
python
Ciel_752125 分钟前
AmazeVault 核心功能分析,认证、安全和关键的功能
python·pyqt·pip
没有了遇见1 小时前
Android 原生定位实现(替代融合定位收费,获取经纬度方案)
android·kotlin
一枚小小程序员哈1 小时前
基于Android的车位预售预租APP/基于Android的车位租赁系统APP/基于Android的车位管理系统APP
android·spring boot·后端·struts·spring·java-ee·maven
诸神黄昏EX1 小时前
Android SystemServer 系列专题【篇四:SystemServerInitThreadPool线程池管理】
android
王国强20091 小时前
Python 异步编程的原理与实践
python
用户2018792831672 小时前
pm path 和 dumpsys package 的区别
android
是店小二呀2 小时前
【C++】智能指针底层原理:引用计数与资源管理机制
android·java·c++
不枯石2 小时前
Python实现RANSAC进行点云直线、平面、曲面、圆、球体和圆柱拟合
python·计算机视觉