问题描述
我正在尝试创建短信对话列表.我正在使用以下代码来获取光标:
I am trying to create an sms conversation list. I am using this code to get a cursor:
Cursor cursor = activity.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
问题是,当我使用光标时,我从一个联系人那里收到多条消息.我试图仅从所有联系人中获取最新消息,以便可以在recyclerview中将其显示为对话列表.
The problem is that when I use the cursor, I get multiple messages from one contact. I am trying to get only the most recent message from all contacts so that I can show them in a recyclerview as a list of conversations.
提前谢谢!
推荐答案
查询"content://sms/conversations"
URI( Telephony.Sms.Conversations.CONTENT_URI
)将返回每个对话的摘要,其中"snippet"
列( Telephony.Sms.Conversations.SNIPPET
)是每个对话中的最后一条消息.
Querying the "content://sms/conversations"
URI (Telephony.Sms.Conversations.CONTENT_URI
) will return a summary of each conversation, with the "snippet"
column (Telephony.Sms.Conversations.SNIPPET
) being the last message in each.
此查询还将返回一个"msg_count"
列( Telephony.Sms.Conversations.MESSAGE_COUNT
)-这很不言自明-和一个"thread_id"
列( Telephony.Sms.Conversations.THREAD_ID
),可通过查询附加在对话URI上的ID来检索完整的对话.例如:
This query will also return with a "msg_count"
column (Telephony.Sms.Conversations.MESSAGE_COUNT
) - which is pretty self-explanatory - and a "thread_id"
column (Telephony.Sms.Conversations.THREAD_ID
), which can be used to retrieve a complete conversation, by querying with that ID appended to the conversations URI. For example:
String threadId = ...
Uri convoUri = Telephony.Sms.Conversations.CONTENT_URI
.buildUpon().appendPath(threadId).build();
请注意,这些查询将仅返回SMS消息.如果还需要MMS,则 Telephony.Mms
和 Telephony.MmsSms
具有相似的URI.
Do note that these queries will return only SMS messages. If you want MMS as well, Telephony.Mms
and Telephony.MmsSms
have similar URIs.
这篇关于如何仅在对话视图中使用最新的SMS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!