歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 獲取短信會話信息

Android 獲取短信會話信息

日期:2017/3/1 10:47:36   编辑:Linux編程

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字段。

  1. Cursor cursor = cr.query(Uri.parse("content://sms/"),
  2. new String[] { "* from threads--" }, null, null, null);

查詢Threads表。

網上說Threads的URI為:"content://mms-sms/conversations"

不過由於本人使用這個Uri查詢出錯,故使用content://sms/ 通過構造查詢字段數組來查詢Threads表。

  1. public static List<Threads> getSession(ContentResolver cr) {
  2. Cursor cursor = cr.query(Uri.parse("content://sms/"),
  3. new String[] { "* from threads--" }, null, null, null);
  4. list = new ArrayList<Threads>();
  5. if (cursor.moveToFirst()) {
  6. do {
  7. if (threads == null) {
  8. threads = new Threads();
  9. }
  10. threads.set_id(cursor.getInt(ID));
  11. threads.setDate(cursor.getLong(DATE));
  12. threads.setError(cursor.getInt(ERROR));
  13. threads.setHas_attachment(cursor.getInt(HAS_ATTACHMENT));
  14. threads.setMessage_count(cursor.getInt(MESSAGE_COUNT));
  15. threads.setRead(cursor.getInt(READ));
  16. threads.setRecipient_ids(cursor.getString(RECIPIENT_IDS));
  17. threads.setSnippet(cursor.getString(SNIPPET));
  18. threads.setSnippet_cs(cursor.getInt(SNIPPET_CS));
  19. threads.setType(cursor.getInt(TYPE));
  20. list.add(threads);
  21. threads = null;
  22. } while (cursor.moveToNext());
  23. }
  24. return list;
  25. }

最後通過獲取到的thread_id作為參數再去查詢sms表。就可以獲取每個會話的所有信息。

  1. package wu.lis.bu.utils;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import wu.lis.bu.bean.Status;
  7. import android.content.ContentResolver;
  8. import android.database.Cursor;
  9. import android.net.Uri;
  10. import android.util.Log;
  11. public class SmsService {
  12. private final String SMS_URI_ALL = "content://sms/";
  13. private final String SMS_URI_INBOX = "content://sms/inbox";
  14. private final String SMS_URI_SEND = "content://sms/send";
  15. private final String SMS_URI_DRAFT = "content://sms/draft";
  16. List<Status> sms_list = null;
  17. Status status = null;
  18. public List<Status> getSmsInphone(ContentResolver cr, Integer thread_id) {
  19. sms_list = new ArrayList<Status>();
  20. String[] projection = new String[] { "_id", "address", "person",
  21. "body", "date", "type" };
  22. Uri uri = Uri.parse(SMS_URI_ALL);
  23. Cursor cursor = cr.query(uri, projection, "thread_id=?",
  24. new String[] { Integer.toString(thread_id) }, "date desc");
  25. if (cursor.moveToFirst()) {
  26. String name;
  27. String phoneNumber;
  28. String smsBody;
  29. String date;
  30. String type;
  31. //int nameColumn = cursor.getColumnIndex("person");
  32. int phoneNumberColumn = cursor.getColumnIndex("address");
  33. int smsBodyColumn = cursor.getColumnIndex("body");
  34. int dateColumn = cursor.getColumnIndex("date");
  35. int typeColumn = cursor.getColumnIndex("type");
  36. do {
  37. status = new Status();
  38. //name = cursor.getString(nameColumn);
  39. String pNumber = "";
  40. phoneNumber = cursor.getString(phoneNumberColumn);
  41. if (phoneNumber.length() > 11) {
  42. pNumber = phoneNumber.substring(phoneNumber.length() - 11,
  43. phoneNumber.length());
  44. } else {
  45. pNumber = phoneNumber;
  46. }
  47. name = PhoneService.getPeople(cr, pNumber);
  48. smsBody = cursor.getString(smsBodyColumn);
  49. SimpleDateFormat dateFormat = new SimpleDateFormat(
  50. "yyyy-MM-dd hh:mm:ss");
  51. Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));
  52. date = dateFormat.format(d);
  53. int typeId = cursor.getInt(typeColumn);
  54. if (typeId == 1) {
  55. type = "接收";
  56. } else if (typeId == 2) {
  57. type = "發送";
  58. } else {
  59. type = "";
  60. }
  61. if (smsBody == null) {
  62. smsBody = "";
  63. }
  64. status.setPhoneNum(phoneNumber);
  65. status.setContent(smsBody);
  66. status.setLastReceive(date);
  67. status.setPerson(name);
  68. status.settype(type);
  69. sms_list.add(status);
  70. status = null;
  71. } while (cursor.moveToNext());
  72. }
  73. for (Status status : sms_list) {
  74. Log.i("Status", status.getPhoneNum());
  75. }
  76. return sms_list;
  77. }
  78. }
Copyright © Linux教程網 All Rights Reserved