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