利用contentProvider获取短信内容(SimpleDataFormater的应用)
public static List<SmsInfo> getSmsInfos(Context context){ // 获取到短信内容提供者的uri Uri uri = Uri.parse(path); ContentResolver cr = context.getContentResolver(); //_id , address , body ,date ,type List<SmsInfo> smsinfos = new ArrayList<SmsInfo>(); Cursor cursor = cr.query(uri, new String[]{"_id","address","body","date","type"}, null, null, "date desc"); while(cursor.moveToNext()){ SmsInfo info = new SmsInfo(); String address = cursor.getString(cursor.getColumnIndex("address")); info.setAddress(address); String body = cursor.getString(cursor.getColumnIndex("body")); info.setBody(body); String date = cursor.getString(cursor.getColumnIndex("date")); //定义字符串的日期格式化器 SimpleDateFormat dataFormater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d = new Date(Long.parseLong(date)); String strdate = dataFormater.format(d); info.setDate(strdate); String type = cursor.getString(cursor.getColumnIndex("type")); if("1".equals(type)){ info.setType("发送"); }else if("2".equals(type)){ info.setType("接受"); } smsinfos.add(info); } return smsinfos; }