Android 手机信息存放在mmssms.db数据库。
短讯息主要用到sms表和threads表。
查看其表结构
sms表,信息表
threads表
1.mesage_count该会话的消息数量
2.recipient_ids为联系人ID,这个ID不是联系人表中的_id,而是指向表canonical_address里的id,
canonical_address这个表同样位于mmssms.db,它映射了recipient_ids到一个电话号码,也就是说,
最终获取联系人信息,还是得通过电话号码;
3.snippet为最后收到/发出的信息
4._id为会话id,他关联到sms表中的thread_id字段。
Cursor cursor = cr.query(Uri.parse("content://sms/"), new String[] { "* from threads--" }, null, null, null);
查询Threads表。
网上说Threads的URI为:"content://mms-sms/conversations"
不过由于本人使用这个Uri查询出错,故使用content://sms/ 通过构造查询字段数组来查询Threads表。
public static List<Threads> getSession(ContentResolver cr) { Cursor cursor = cr.query(Uri.parse("content://sms/"), new String[] { "* from threads--" }, null, null, null); list = new ArrayList<Threads>(); if (cursor.moveToFirst()) { do { if (threads == null) { threads = new Threads(); } threads.set_id(cursor.getInt(ID)); threads.setDate(cursor.getLong(DATE)); threads.setError(cursor.getInt(ERROR)); threads.setHas_attachment(cursor.getInt(HAS_ATTACHMENT)); threads.setMessage_count(cursor.getInt(MESSAGE_COUNT)); threads.setRead(cursor.getInt(READ)); threads.setRecipient_ids(cursor.getString(RECIPIENT_IDS)); threads.setSnippet(cursor.getString(SNIPPET)); threads.setSnippet_cs(cursor.getInt(SNIPPET_CS)); threads.setType(cursor.getInt(TYPE)); list.add(threads); threads = null; } while (cursor.moveToNext()); } return list; }
最后通过获取到的thread_id作为参数再去查询sms表。就可以获取每个会话的所有信息。
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; } }