If you want to use contentprovider to query text messages, you can use group by in contentresolver. query to find that the system does not provide interfaces or available fields.
Exploring
First, let's look at the query function:
public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return query(uri, projection, selection, selectionArgs, sortOrder, null);}
The most likely thing to handle is selection. First, we try to setselection = "gourp by thread_id"Execute the program and find from the error log that the SQL statement is compiled with brackets, as shown below:
SELECT _id, thread_id, address, person, body, date FROM sms WHERE (type=1) AND (group by thread_id) ORDER BY date DESC
It seems that to get the correct SQL statement, the selection settings need to be more skillful. Try againselection = "0=0) group by (thread_id"This time! We mainly pay attention to the processing of parentheses, and there must be a query condition behind and. Here we use0=0(Verify here0==0This permanent query is only used to ensure the correctness of SQL statements.
The final processing result is as follows:
Uri SMS_PROVIDER = Uri.parse("content://sms/inbox");String[] projection = new String[] { "_id", "thread_id", "address", "person", "body", "date"};Cursor cursor = context.getContentResolver().query(SMS_PROVIDER, projection, "0=0) group by (thread_id", null, "date desc");
In this way, the compiled SQL statement is:
SELECT _id, thread_id, address, person, body, date FROM sms WHERE (type=1) AND (0=0)group by (thread_id) ORDER BY date DESC
Use group by in contentresolver