我是这样做的。创建短信对象:
public class SMSObject {
private String _id;
private String _address;
private String _msg;
private String _readState; // "0" for have not read sms and "1" for have
// read sms
private String _time;
private String _folderName;
//+ getter and setter methods and
@Override
public String toString() {
return "SMSObject [_id=" + _id + ", _address=" + _address + ", _msg="
+ _msg + ", _readState=" + _readState + ", _time=" + _time
+ ", _folderName=" + _folderName + "]";
}
这里有一个简单的函数,它简单地记录所有当前的 SMS-Objects
private void readSMS() {
List<SMSObject> lstSms = new ArrayList<SMSObject>();
SMSObject objSms = new SMSObject();
Uri message = Uri.parse("content://sms/");
ContentResolver cr = this.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
// this.startManagingCursor(c);
int totalSMS = c.getCount();
Log.d("SMS Count->", "" + totalSMS);
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new SMSObject();
objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
objSms.setAddress(c.getString(c
.getColumnIndexOrThrow("address")));
objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
objSms.setReadState(c.getString(c.getColumnIndex("read")));
objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
objSms.setFolderName("inbox");
} else {
objSms.setFolderName("sent");
}
lstSms.add(objSms);
Log.d("SMS at " + i, objSms.toString());
c.moveToNext();
}
}
// else {
// throw new RuntimeException("You have no SMS");
// }
c.close();
// return lstSms;
}