package wu.lis.bu.utils; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import wu.lis.bu.bean.Status; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.util.Log; public class SmsService { private final String SMS_URI_ALL = "content://sms/"; private final String SMS_URI_INBOX = "content://sms/inbox"; private final String SMS_URI_SEND = "content://sms/send"; private final String SMS_URI_DRAFT = "content://sms/draft"; List<Status> sms_list = null; Status status = null; public List<Status> getSmsInphone(ContentResolver cr, Integer thread_id) { sms_list = new ArrayList<Status>(); String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; Uri uri = Uri.parse(SMS_URI_ALL); Cursor cursor = cr.query(uri, projection, "thread_id=?", new String[] { Integer.toString(thread_id) }, "date desc"); if (cursor.moveToFirst()) { String name; String phoneNumber; String smsBody; String date; String type; //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"); do { status = new Status(); //name = cursor.getString(nameColumn); String pNumber = ""; phoneNumber = cursor.getString(phoneNumberColumn); if (phoneNumber.length() > 11) { pNumber = phoneNumber.substring(phoneNumber.length() - 11, phoneNumber.length()); } else { pNumber = phoneNumber; } name = PhoneService.getPeople(cr, pNumber); smsBody = cursor.getString(smsBodyColumn); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(Long.parseLong(cursor.getString(dateColumn))); date = dateFormat.format(d); int typeId = cursor.getInt(typeColumn); if (typeId == 1) { type = "接收"; } else if (typeId == 2) { type = "发送"; } else { type = ""; } if (smsBody == null) { smsBody = ""; } status.setPhoneNum(phoneNumber); status.setContent(smsBody); status.setLastReceive(date); status.setPerson(name); status.settype(type); sms_list.add(status); status = null; } while (cursor.moveToNext()); } for (Status status : sms_list) { Log.i("Status", status.getPhoneNum()); } return sms_list; } }
Android 获取短信会话信息(2)
内容版权声明:除非注明,否则皆为本站原创文章。