利用contentProvider获取短信内容(SimpleDataFormater的应用)

来源:互联网 发布:网络销售邮币卡可靠吗 编辑:程序博客网 时间:2024/05/21 03:55

http://www.cnblogs.com/ligang305/archive/2012/07/20/2600467.html


     
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;    }