Android进阶篇-抓取系统短信信息

短信信息都储存在系统的数据库中,如果我们想获取相关信息需要根据URI进行查找操作.

定义一个实体接收类SmsInfo.java:

public class SmsInfo {
    private String smsbody;//短信内容
    private String phoneNumber;//发送短信的电话号码
    private String date;//发送短信的日期和时间
    private String name;//发送者的名字
    private String type;//短信类型   1 接收到的    2 已发出的 
    
    public SmsInfo() {
        super();
    }

    public SmsInfo(String smsbody, String phoneNumber, String date,
            String name, String type) {
        super();
        this.smsbody = smsbody;
        this.phoneNumber = phoneNumber;
        this.date = date;
        this.name = name;
        this.type = type;
    }

    public String getSmsbody() {
        return smsbody;
    }

    public void setSmsbody(String smsbody) {
        this.smsbody = smsbody;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getDate() {
        return date;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

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

    @Override
    public String toString() {
        return "SmsInfo [smsbody=" + smsbody + ", phoneNumber=" + phoneNumber
                + ", date=" + date + ", name=" + name + ", type=" + type + "]";
    }
}

查找短信的URI:

    /**
     * 所有的短信
     */
    public static final String SMS_URI_ALL = "content://sms/";
    /**
     * 收件箱短信
     */
    public static final String SMS_URI_INBOX = "content://sms/inbox";
    /**
     * 发件箱短信
     */
    public static final String SMS_URI_SEND = "content://sms/sent";
    /**
     * 草稿箱短信
     */
    public static final String SMS_URI_DRAFT = "content://sms/draft";

查询类SmsContent.java:

public class SmsContent {
    private Activity activity;
    private Uri uri;
    private List<SmsInfo> infos;
    
    public SmsContent(Activity activity,Uri uri){
        this.activity = activity;
        this.uri = uri;
        infos = new ArrayList<SmsInfo>();
    }
    
    /**
     * 获取短信的各种信息 
     */
    public List<SmsInfo> getSmsInfo() {
        String[] projection = new String[] { "_id", "address", "person","body", "date", "type"};
        Cursor cusor = activity.managedQuery(uri, projection, null, null,"date desc");
        
        int nameColumn = cusor.getColumnIndex("person");
        int phoneNumberColumn = cusor.getColumnIndex("address");
        int smsbodyColumn = cusor.getColumnIndex("body");
        int dateColumn = cusor.getColumnIndex("date");
        int typeColumn = cusor.getColumnIndex("type");
        
        if (cusor != null) {
            while (cusor.moveToNext()) {
                SmsInfo smsinfo = new SmsInfo();
                smsinfo.setName(cusor.getString(nameColumn));
                smsinfo.setDate(cusor.getString(dateColumn));
                smsinfo.setPhoneNumber(cusor.getString(phoneNumberColumn));
                smsinfo.setSmsbody(cusor.getString(smsbodyColumn));
                smsinfo.setType(cusor.getString(typeColumn));
                infos.add(smsinfo);
            }
            cusor.close();
        }
        return infos;
    }
}

最后加上权限:

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

 

 

posted @ 2013-01-26 10:36  暗殇  阅读(438)  评论(0编辑  收藏  举报