Android 获取Sms

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

1: 申请权限

<uses-permission android:name="android.permission.READ_SMS" />

2: 获取短信内容列表

    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手机为例:

    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的所有字段:

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: 【北京移动】尊敬的客户,您的202311月积分账单已送达,点击查看账单详情 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: -----------------------------------------------
03-09 00:11