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

public static List getSmsInfos(Context context){
		// 获取到短信内容提供者的uri
		Uri uri = Uri.parse(path);
		ContentResolver  cr = context.getContentResolver(); 
		
		//_id , address , body ,date ,type 
		List smsinfos = new ArrayList();
		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;
	}
	
	

  

转载于:https://www.cnblogs.com/ligang305/archive/2012/07/20/2600467.html

你可能感兴趣的:(利用contentProvider获取短信内容(SimpleDataFormater的应用))