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);
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.
Thanks in advance!
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.
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();
Do note that these queries will return only SMS messages. If you want MMS as well, Telephony.Mms and Telephony.MmsSms have similar URIs.
Related
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);
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.
Thanks in advance!
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.
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();
Do note that these queries will return only SMS messages. If you want MMS as well, Telephony.Mms and Telephony.MmsSms have similar URIs.
Goal: I'm trying to group SMS and calls from same (known) contacts.
I read SMS using Telephony.Sms.Inbox.CONTENT_URI and Telephone.Sms.Sent.CONTENT_URI.
I read calls using CallLog.Calls.CONTENT_UTI.
I get the contact's ID by following android is CallLog.Calls._ID the same with the contact id?: using Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, phoneNumber).
Now, having this PhoneLookup._ID, I want to list SMS from that contact.
Sadly Telephony.Sms.PERSON does not seem to store the same contact ID.
Question: How could I list SMS from that PhoneLookup._ID or if not possible, how could I achieve what I want: list SMS and calls from the same contact.
PS: Telephony.TextBasedSmsColumns.PERSON seems to refer the the deprecated Contacts.People API...
Telephony.Sms.PERSON actually matchs ContactsContract.DataColumns.RAW_CONTACT_ID.
So I used:
final Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, null);
Reading ContactsContract.DataColumns.RAW_CONTACT_ID.
https://developer.android.com/reference/android/provider/ContactsContract.DataColumns.html#RAW_CONTACT_ID
When I query the SMS provider in Android I get the messages in from the older ones to the most recent ones. I am using the following query:
Cursor cursor = getContentResolver().query(messagesUri,null,null,null,null);
Is it possible to sort it the opposite way?
The solution is to transform the last argument from null to date ASC:
Cursor cursor = getContentResolver().query(messagesUri,null,null,null,"date ASC");
I am currently using
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://sms/");
Cursor messagesCursor = cr.query(uri, new String[] { "_id", "address", "body", "person" }, null, null, null);
if (messagesCursor.getCount() > 0) {
while (messagesCursor.moveToNext()) {
colName = getContactName(messagesCursor.getString(messagesCursor.getColumnIndex("address"))) + "\n";
}
}
to get all the text in the user's inbox. From there, messagesCursor.getColumnIndex("address") will get the SMS's number. Then it will be place in getContactName() where it will then get the contacts' name from their contact's list.
This method will take a long time as it goes though EVERY SINGLE text message, get number, then get the contact's name, check if name exist in array, if it doesn't put in array then from there create a ListView.
The app will take a long time to load on start up if the user's inbox contains lots of text.
Is there any special way to get UNIQUE numbers from inbox? Something in cr.query?
Thanks in advance.(:
EDIT :
I'm creating just a simple inbox application. It reads from the original inbox message and list the messages in it.
For this project, what I did was to read through ALL the "address" which is the phone number, put it in an ArrayList, and finally to the ListView. As it cycles through all the messages in inbox, it takes a long time to startup.
For example :
Inbox Messages
Person 1
Person 1
Person 1
Person 2
Person 2
Person 3
Person 3
Person 3
Person 3
What my current code does :
Read EVERY SINGLE message
Check if name in ArrayList
If it is, skip. If not, add in array.
Throw ArrayList into ListView.
What my intention was :
A code that only reads out :
Person 1
Person 2
Person 3
Try only using "address" in projection (that means, don't include even "_id") - I'm not sure about this but it may solve the case.
I'm developing an app which requires threaded sms. I was able to retrieve contents from inbox, but in the threaded view sms must be filled with both inbox and sent items.
Separately both content://sms/inbox and content://sms/sent are working well.
How do I join contents from two URI's and order by time?
Can I use content://sms/all?
Null value is returned for cursor when ALL CONTENT URI is used.
How to do this?
At last found the answer for this..
content://sms/all
is something which i couldnt find.
But for retrieving both sent and received we can use
Uri selectUri = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(selectUri,null,"thread_id="+threadid, null,"DATE desc");
This snippet fetches and displays in descending order
Thanks all
I had the same issue. For this,you can use MatrixCursor.What I have done is-
Get all sms from content://sms/inbox for a thread_id
Get all sms from content://sms/sent for a thread_id
Maintain an arraylist and sort them in the order you want(I did this using bubble sort)
Now define and initialize the matrixCursor
(Refer this: http://groups.google.com/group/android-developers/browse_thread/thread/470dd3a1703848eb/d7e70618ce413261?q=MatrixCursor+join+two+tables for MatrixCursor )
Add all the sorted records to your matrixCursor
(Please note that adding this record should be in the sequence of at what time and from which folder(inbox or sent) they come.MatrixCursor simply lets you create a custom cursor so you need to maintain the sequence.)