SMS in Android does not have an official content provider available and no definition is provided in the official documentation. However, you can still define the URI yourself and then query the content of the text message. For example, conetent://sms is the path where all text messages are located.
The text messages need to be classified by session. I originally found out all text messages and then classified by thread_id. The system's own SMS program includes a session display interface, each entry includes: contact person, number of SMS, first SMS, etc. When my program processes more text messages, it becomes very slow to query all text messages at once. (It will be slower if you add querying contact information for each session)
After reading the system text message code, I found that it can only query the session information without querying all text message content. Because some of the code was not found, I have never known how it was done. After reading the code of the telphony provider, I learned a little bit.
In fact, there is no table in the SMS database that stores session information. The content provider provided by the system actually supports direct query of session information. However, its implementation method is not achieved through a ready-made table, but through SQL statements to retrieve data from multiple tables. This implementation is also mentioned in this post.
I won't go into the implementation method, after all, I'm not very familiar with SQL queries. Release the direct usage method:
Get session information URI
Java code
public static final Uri MMSSMS_FULL_CONVERSATION_URI = ("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 = ("content://mms-sms/conversations"); public static final Uri CONVERSATION_URI = MMSSMS_FULL_CONVERSATION_URI.buildUpon(). appendQueryParameter("simple", "true").build();
By specifying simple=true, you can get an approximate session data, including the following columns:
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 names are:
Java code
private static final String[] ALL_THREADS_PROJECTION = { "_id", "date", "message_count", "recipient_ids", "snippet", "snippet_cs", "read", "error", "has_attachment" };
in:
1. message_count is the number of messages for the session;
2. Recipient_ids is the contact ID. This ID is not the _id in the contact table, but points to the id in the table canonical_addresses. The canonical_addresses table is also located. It maps recipient_ids to a phone number, that is, in the end, you have to use the phone number to obtain contact information;
3. Snippet is the last text message received/send;
The type of each data is roughly:
Java code
long id = (ID); long date = (DATE); long msgCount = (MESSAGE_COUNT); String recipIDs = (RECIPIENT_IDS); String snippet = (SNIPPET); long snippetCS = (SNIPPET_CS); long read = (READ); long type = (TYPE); long error = (ERROR); long hasAttach = (HAS_ATTACHMENT); long id = (ID); long date = (DATE); long msgCount = (MESSAGE_COUNT); String recipIDs = (RECIPIENT_IDS); String snippet = (SNIPPET); long snippetCS = (SNIPPET_CS); long read = (READ); long type = (TYPE); long error = (ERROR); long hasAttach = (HAS_ATTACHMENT);
The above content is the full description of the Android SMS session list introduced by the editor. I hope you like it.