获取手机中所有的Message, 并使用for循环遍历输出;
//获取手机中所有的Message;
private List<Map<String, Object>> getMsgInPhone()
{
final String MSG_URI_ALL = "content://sms/"; //所有信息对应的message;
List<Map<String, Object>> contentsList = new ArrayList<Map<String, Object>>();
Uri uri = null;
uri = Uri.parse(MSG_URI_ALL);
//所要查询的列;
String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
Cursor cursor = getContentResolver().query(uri, projection, null, null, "date desc"); //获取手机内部短信;
while(cursor.moveToNext())
{
Map<String, Object> map = new HashMap<String, Object>();
String strAddress = cursor.getString(cursor.getColumnIndex("address"));//取得短信发送目标的电话;
int intPerson = cursor.getInt(cursor.getColumnIndex("person")); //取得索引;
String strBody = cursor.getString(cursor.getColumnIndex("body")); //取得信息的内容;
long longDate = cursor.getLong(cursor.getColumnIndex("date")); //取得日期;
int intType = cursor.getInt(cursor.getColumnIndex("type")); //取得短信的类型,1表示已接收,2表示已发送;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化日期;
Date date = new Date(longDate);
String strDate = sdf.format(date);
map.put("listNum", strAddress);
map.put("listMsg", strBody);
map.put("listTime", strDate);
if(intType == 1)
{
map.put("listType", "收");
}
else if(intType == 2)
{
map.put("listType", "发");
}
contentsList.add(map);
}
//打印输出信息;
for(int i=0; i<contentsList.size(); i++)
{
Map maps = contentsList.get(i);
Set set = maps.keySet();
for(Object x:set)
{
System.out.println(x + "-----" + maps.get(x));
}
}
if(!cursor.isClosed())
{
cursor.close();
cursor = null;
}
return contentsList;
}