android获取短信所有内容 – liuyiming_的专栏 – 博客频道 – CSDN.NET

一个获取短信的类,可以参考下其中的写法。

public class GetMessageInfo {

List<MessageInfo> list;

Context context;

MessageInfo messageInfo;

public GetMessageInfo(Context context) {

list = new ArrayList<MessageInfo>();

this.context = context;

}

// ——————————–收到的短息信息———————————-

public List<MessageInfo> getSmsInfos() {

final String SMS_URI_INBOX = “content://sms/inbox”;// 收信箱

try {

ContentResolver cr = context.getContentResolver();

String[] projection = new String[] { “_id”, “address”, “person”,”body”, “date”, “type” };

Uri uri = Uri.parse(SMS_URI_INBOX);

Cursor cursor = cr.query(uri, projection, null, null, “date desc”);

while (cursor.moveToNext()) {

messageInfo = new MessageInfo();

// ———————–信息—————-

int nameColumn = cursor.getColumnIndex(“person”);// 联系人姓名列表序号

int phoneNumberColumn = cursor.getColumnIndex(“address”);// 手机号

int smsbodyColumn = cursor.getColumnIndex(“body”);// 短信内容

int dateColumn = cursor.getColumnIndex(“date”);// 日期

int typeColumn = cursor.getColumnIndex(“type”);// 收发类型 1表示接受 2表示发送

String nameId = cursor.getString(nameColumn);

String phoneNumber = cursor.getString(phoneNumberColumn);

String smsbody = cursor.getString(smsbodyColumn);

Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));

SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd ” + “\n” + “hh:mm:ss”);

String date = dateFormat.format(d);

// ————————–匹配联系人名字————————–

Uri personUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,phoneNumber);

Cursor localCursor = cr.query(personUri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID,PhoneLookup._ID }, null, null, null);

 

System.out.println(localCursor.getCount());

System.out.println(“之前—-“+localCursor);

if (localCursor.getCount()!=0) {

localCursor.moveToFirst();

System.out.println(“之后—-“+localCursor);

String name = localCursor.getString(localCursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));

long photoid = localCursor.getLong(localCursor.getColumnIndex(PhoneLookup.PHOTO_ID));

long contactid = localCursor.getLong(localCursor.getColumnIndex(PhoneLookup._ID));

messageInfo.setName(name);

// 如果photoid 大于0 表示联系人有头像 ,如果没有给此人设置头像则给他一个默认的

if (photoid > 0) {

Uri uri1 = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);

InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri1);

messageInfo.setContactPhoto(BitmapFactory.decodeStream(input));

} else {

messageInfo.setContactPhoto(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher));

}

}else{

messageInfo.setName(phoneNumber);

messageInfo.setContactPhoto(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));

}

 

localCursor.close();

 

messageInfo.setSmsContent(smsbody);

messageInfo.setSmsDate(date);

list.add(messageInfo);

}

} catch (SQLiteException e) {

e.printStackTrace();

}

return list;

}

}

来源URL:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461f1c21a8e078794c51c4c2087a52ef0f07bbae616f615466e4c096c957d8bc942f2b8c21203541c6171d8045f98b1b7e9737912afedf1df0cbf325e2a4c5a0dc4352ba44757e9781fa4d7616dd1ef7&p=8b2a9713c5d51eff57ed9779474ba5&newp=90759a45d5c406eb0be2962c4f0093231610db2151d1d7523b&user=baidu&fm=sc&query=android%B6%C1%C8%A1%B6%CC%D0%C5&qid=cb8ea330000f68db&p1=4