读取短信

    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>

 

posted @ 2015-10-28 14:07  一路向北中  阅读(162)  评论(0)    收藏  举报