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: -----------------------------------------------
相关推荐
前行的小黑炭1 小时前
【Android】 Context使用不当,存在内存泄漏,语言不生效等等
android·kotlin·app
前行的小黑炭2 小时前
【Android】CoordinatorLayout详解;实现一个交互动画的效果(上滑隐藏,下滑出现);附例子
android·kotlin·app
databook13 小时前
Manim实现闪光轨迹特效
后端·python·动效
用户20187928316714 小时前
Android黑夜白天模式切换原理分析
android
芦半山14 小时前
「幽灵调用」背后的真相:一个隐藏多年的Android原生Bug
android
Juchecar15 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
卡尔特斯15 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
ace望世界15 小时前
安卓的ViewModel
android
ace望世界15 小时前
kotlin的委托
android
用户83562907805115 小时前
Python 删除 Excel 工作表中的空白行列
后端·python