歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android短信和彩信探秘threads

Android短信和彩信探秘threads

日期:2017/3/1 11:17:01   编辑:Linux編程

Android源代碼在 彩信sms 表和彩信 pdu表,增加了一個觸發器

  1. CREATE TRIGGER delete_obsolete_threads_pdu AFTER
  2. DELETE ON pdu BEGIN
  3. DELETE FROM threads
  4. WHERE _id = old.thread_id AND _id NOT IN
  5. (SELECT thread_id FROM sms UNION SELECT thread_id from pdu);
  6. END

仔細看下就明白,如果threads表沒有sms和pdu外部引用的時候,這條thread就會被刪除。

thread被刪除後,你再插入一條短信或者彩信(當然是代碼插入),這時候因為沒有thread id,所以就會不顯示。

有人可能想到對threads表一起進行維護不就行了嗎? 很不幸 ,系統對這個表的providers並不完全開放,只能用於查找。

但我們這時候又需要thread_id, 我對源代碼進行了一些修改,把thread類提取了出來,僅供大家參考

  1. package com.sweetop.provider;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import android.util.Patterns;
  12. public final class Threads implements ThreadsColumns {
  13. private static final String[] ID_PROJECTION = { BaseColumns._ID };
  14. private static final String STANDARD_ENCODING = "UTF-8";
  15. private static final Uri THREAD_ID_CONTENT_URI = Uri
  16. .parse("content://mms-sms/threadID");
  17. public static final Uri CONTENT_URI = Uri.withAppendedPath(
  18. Uri.parse("content://mms-sms/"), "conversations");
  19. public static final Uri OBSOLETE_THREADS_URI = Uri.withAppendedPath(
  20. CONTENT_URI, "obsolete");
  21. public static final Pattern NAME_ADDR_EMAIL_PATTERN = Pattern
  22. .compile("\\s*(\"[^\"]*\"|[^<>\"]+)\\s*<([^<>]+)>\\s*");
  23. public static final int COMMON_THREAD = 0;
  24. public static final int BROADCAST_THREAD = 1;
  25. // No one should construct an instance of this class.
  26. private Threads() {
  27. }
  28. /**
  29. * This is a single-recipient version of getOrCreateThreadId. It's
  30. * convenient for use with SMS messages.
  31. */
  32. public static long getOrCreateThreadId(Context context, String recipient) {
  33. Set<String> recipients = new HashSet<String>();
  34. recipients.add(recipient);
  35. return getOrCreateThreadId(context, recipients);
  36. }
  37. /**
  38. * Given the recipients list and subject of an unsaved message, return its
  39. * thread ID. If the message starts a new thread, allocate a new thread ID.
  40. * Otherwise, use the appropriate existing thread ID.
  41. *
  42. * Find the thread ID of the same set of recipients (in any order, without
  43. * any additions). If one is found, return it. Otherwise, return a unique
  44. * thread ID.
  45. */
  46. public static long getOrCreateThreadId(Context context,
  47. Set<String> recipients) {
  48. Uri.Builder uriBuilder = THREAD_ID_CONTENT_URI.buildUpon();
  49. for (String recipient : recipients) {
  50. if (isEmailAddress(recipient)) {
  51. recipient = extractAddrSpec(recipient);
  52. }
  53. uriBuilder.appendQueryParameter("recipient", recipient);
  54. }
  55. Uri uri = uriBuilder.build();
  56. // if (DEBUG) Log.v(TAG, "getOrCreateThreadId uri: " + uri);
  57. Cursor cursor = context.getContentResolver().query(uri, ID_PROJECTION,
  58. null, null, null);
  59. if (true) {
  60. Log.v("Threads",
  61. "getOrCreateThreadId cursor cnt: " + cursor.getCount());
  62. }
  63. if (cursor != null) {
  64. try {
  65. if (cursor.moveToFirst()) {
  66. return cursor.getLong(0);
  67. } else {
  68. Log.e("Threads", "getOrCreateThreadId returned no rows!");
  69. }
  70. } finally {
  71. cursor.close();
  72. }
  73. }
  74. Log.e("Threads",
  75. "getOrCreateThreadId failed with uri " + uri.toString());
  76. throw new IllegalArgumentException(
  77. "Unable to find or allocate a thread ID.");
  78. }
  79. public static String extractAddrSpec(String address) {
  80. Matcher match = NAME_ADDR_EMAIL_PATTERN.matcher(address);
  81. if (match.matches()) {
  82. return match.group(2);
  83. }
  84. return address;
  85. }
  86. /**
  87. * Returns true if the address is an email address
  88. *
  89. * @param address
  90. * the input address to be tested
  91. * @return true if address is an email address
  92. */
  93. public static boolean isEmailAddress(String address) {
  94. if (TextUtils.isEmpty(address)) {
  95. return false;
  96. }
  97. String s = extractAddrSpec(address);
  98. Matcher match = Patterns.EMAIL_ADDRESS.matcher(s);
  99. return match.matches();
  100. }
  101. }
當你需要獲得一個thread id時,僅需調用
  1. Threads.getOrCreateThreadId(this, address)

address是發送方的手機地址

Copyright © Linux教程網 All Rights Reserved