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");
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.
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.
I am trying to maintain a contact database and get a callback for Add/Update/Delete as soon as something changes in the URI.
I have written a ContentObserver to observe on ContactsContract.Contacts.CONTENT_URI on contacts. I get a callback as soon as a contact changes and then I update my database by checking ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP.
While this works fine for add/update, it does does not work for deleting a contact.
I do not want to parse all the contacts that I have in memory and check against android database. That would take time and CPU.
I know there exists many question of these types but I am not able to figure out things specific to deleting the contact.
Does there exist a way to perform this ?
As I have posted in above comment as well, following code works for API level 18 and above.
You can query on a uri ContactsContract.DeletedContacts.CONTENT_URI to get the list of all the contacts that have been deleted.
My query looks like following :
String selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?";
String[] selectionArgs = new String[]{String.valueOf(mLastContactDeleteTime)};
Cursor cursor = mContext.getContentResolver().query(ContactsContract.DeletedContacts.CONTENT_URI, null, selection, selectionArgs, null);
I wrote a select query to access set of records from database by setting null for the argument 'orderBy' in the query(). I found that order of records returned by query() method when I run the application in mobile is completely different when I run the same sample application in tablet.
My Query:
Cursor cursor = database.query(true, tableName, downloadQueueTableColumnNames, selection, null, null, null, null, null);
Here, in the query orderBy field is null.
I hope someone to explain the reason behind this...
If you aren't ordering the results, they can be returned in any order. The same device doesn't have to give the same order if you call it twice in a row. If you want it in the same order every time, you must use an order by.
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.)