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

更多相关文章

  1. Android自定义属性时TypedArray的使用方法
  2. android 获取手机通讯录信息
  3. Android联系人数据库全解析(1)
  4. Android(安卓)Contacts的使用(一)
  5. Android自定义属性时TypedArray的使用方法
  6. 初入Android~系统化学习必备(转自http://bbs.weand.com/read-htm-
  7. Android自定义属性时TypedArray的使用方法
  8. gravity和layout_gravity
  9. Read Contacts information in Android(安卓)system.

随机推荐

  1. Golang能使用多核cpu吗
  2. golang的优势是什么?
  3. golang可以热更新吗
  4. Golang能封装成dll吗
  5. go语言和python的区别是什么?
  6. golang和go是一样的吗
  7. Golang可以用来做什么
  8. golint的简易使用方法
  9. golang读取文件是否存在
  10. Golang接口哪里好