Android gets a list of short message sessions

  • 2020-11-30 08:32:29
  • OfStack

Text messages in Android do not have an official content provider available and are not defined in the official documentation. But you can still define URI yourself and then query it for text messages. For example, conetent://sms is the path where all the messages are.

To classify SMS messages by session, I originally searched for all SMS messages and then categorized them according to thread_id. The SMS program in the system contains a session display interface, and each entry contains: contact person, number of SMS messages, the first SMS message, etc. When my program handles a lot of text messages, it becomes slow to query all of them at once. (This is even slower if you add the query for contact information per session)

Looked at the system SMS code, found that it can only query the information of the session, and do not have to query all SMS content. Because part of the code couldn't be found, 1 didn't know how it did it. After looking at the telphony provider code, I know 12.

In fact, there is not a single table in the SMS database (ES21en.db) that stores session information. In content provider provided by the system, it is actually supported to query session information directly. However, this is done not through a ready-made table, but through the SQL statement, fetching data from multiple tables. This implementation is also mentioned in this post.

I won't go into the details of the implementation, as I'm not familiar with SQL queries. Direct usage:

URI to get the session information

Java code


public static final Uri MMSSMS_FULL_CONVERSATION_URI = Uri.parse("content://mms-sms/conversations");
  public static final Uri CONVERSATION_URI = MMSSMS_FULL_CONVERSATION_URI.buildUpon().
  appendQueryParameter("simple", "true").build();
  public static final Uri MMSSMS_FULL_CONVERSATION_URI = Uri.parse("content://mms-sms/conversations");
  public static final Uri CONVERSATION_URI = MMSSMS_FULL_CONVERSATION_URI.buildUpon().
  appendQueryParameter("simple", "true").build();

By specifying simple=true, an approximate session data can be obtained, consisting of the following:

Java code


private static final int ID = 0;
  private static final int DATE = 1;
  private static final int MESSAGE_COUNT = 2;
  private static final int RECIPIENT_IDS = 3;
  private static final int SNIPPET = 4;
  private static final int SNIPPET_CS = 5;
  private static final int READ = 6;
  private static final int TYPE = 7; 
  private static final int ERROR = 8;
  private static final int HAS_ATTACHMENT = 9;

The column name is:

Java code


 private static final String[] ALL_THREADS_PROJECTION = {
  "_id", "date", "message_count", "recipient_ids",
  "snippet", "snippet_cs", "read", "error", "has_attachment"
  };

Among them:

1. message_count is the number of messages in this session;

recipient_ids is the contact person ID. This ID is not the _id in the contact table, but points to the id in the table canonical_addresses. canonical_addresses is also located in mmssms.db.

3. snippet is the last SMS received/sent;

The type of each data is roughly:

Java code


long id = cursor.getLong(ID);
  long date = cursor.getLong(DATE);
  long msgCount = cursor.getLong(MESSAGE_COUNT);
  String recipIDs = cursor.getString(RECIPIENT_IDS);
  String snippet = cursor.getString(SNIPPET);
  long snippetCS = cursor.getLong(SNIPPET_CS);
  long read = cursor.getLong(READ);
  long type = cursor.getLong(TYPE);
  long error = cursor.getLong(ERROR);
  long hasAttach = cursor.getLong(HAS_ATTACHMENT);
  long id = cursor.getLong(ID);
  long date = cursor.getLong(DATE);
  long msgCount = cursor.getLong(MESSAGE_COUNT);
  String recipIDs = cursor.getString(RECIPIENT_IDS);
  String snippet = cursor.getString(SNIPPET);
  long snippetCS = cursor.getLong(SNIPPET_CS);
  long read = cursor.getLong(READ);
  long type = cursor.getLong(TYPE);
  long error = cursor.getLong(ERROR);
  long hasAttach = cursor.getLong(HAS_ATTACHMENT);

The above is the complete description of Android's access to the list of SMS sessions presented by this site. Hope you enjoy it.


Related articles: