读取短信

本文介绍如何在Android应用中请求短信读取权限,并通过ContentResolver查询短信内容,包括日期、正文、发送方地址及类型等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    private void readSMS() {
        ContentResolver resolver = getContentResolver();
        Uri uri = Uri.parse("content://sms/");
        String[] projection = new String[]{"date","body","address","type"};
        Cursor cursor = resolver.query(uri, projection, null, null, null);
        while (cursor.moveToNext()) {
            long date = cursor.getLong(cursor.getColumnIndex("date"));
            String body = cursor.getString(cursor.getColumnIndex("body"));
            String address = cursor.getString(cursor.getColumnIndex("address"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            SMSInfo info = new SMSInfo();
            info.setDate(new Date(date));
            info.setBody(body);
            info.setAddress(address);
            info.setType(type);
            System.out.println(info);
        }
    }
public class SMSInfo {
    Date date;
    String body;
    int type;
    String address;

    @Override
    public String toString() {
        return "SMSInfo{" +
                "date=" + new SimpleDateFormat("yyyy-MM-dd").format(date) +
                ", body='" + body + '\'' +
                ", type=" + type +
                ", address='" + address + '\'' +
                '}';
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>

 

转载于:https://www.cnblogs.com/linson0116/p/4917147.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值