How do I sort SMS and MMS together? - android

I'm implementing the methods discussed here: How to Read MMS Data in Android?
I'm trying to read SMS and MMS into a single listview. I'm doing pretty well, but when I try to sort I'm getting all of the SMS sorted together followed by all of the MMS sorted together.
Here is my code:
Cursor smsCursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations/"), null, null, null, "date DESC");
Can anyone tell me how to combine the two sources or how to combine the MMS into the SMS conversations like the built in Android app does?
Edit: I noticed that the date of SMS has several more digits than the dates of MMS.
Edit 2: Adding "julianday()" like so:
Cursor smsCursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations/"), null, null, null, "julianday(date) DESC");
Makes the MMS appear at the top of the list.

try normalized_date desc instead of date desc for the order.
It should work.

The real problem here seem's to be that in the Database, the date is not stored exactly the same Way for SMS and MMS.
So you'll need to first query the database looking just for the "date" field.
(Not normalized_date that would crash on some devices).
Then sort your list programmaticly in Java, taking in account that for the MMS,
the date needs to be multiplied by 1000 as stated here :
How do the retrieve the date of the mms from content://mms.*
Or here
mms sent/recive date is always in 1970

Related

How to retrieve the number of new contacts added to the contacts database

is there a column in the database of contacts indicates the date added to the base or the last modification date, because I want a method used to retrieve the new add
I found SEVERAL method but I did not understand, I am new to android
Edit : Means how to get the newly updated contacts.
you want to retrieve the number of new contacts since when?
if you adding contacts with your app then just get the number of contacts before you add any and save that value.
then after the user has added contacts get the number again and find the difference between the previous number of contacts. that will be the number of new contacts, but the best way to do it would just be to keep an internal counter of when the user adds contacts.
I think you are looking for something like this, You want to get the updated contacts.
have look here....
Contacts ContentObserver called randomly
Android notify when phone book is updated(Content Observer)
ContentObserver for listening contact changes
Just make this change to your cursor query
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "_id DESC");

android how to tag phone number of a contact programmatically

i did a lot of research but couldn't find anything to help;
my problem is: i need to create an application where the user can select some of the contacts on his phone to be added to this application where he/she can later communicate with them via sms in a special template. but the user need to select only one phone number to be active to this user on this application. this choice must be caved for later logons.
i was able to retrieve contacts and their phone numbers using lookupkey (which will be saved in my application as a reference for preselected users), but i couldn't figure out how to tag the needed phone number, i was thinking of adding a flag to the phone number but i dont know how, i dont know if this is the right way to do it, i thought of setting the selected phone number as primary then query t when needed... or simply save the phone number id (but i am not sure if saving the id is safe in case user changed the phone numer)...
thx for any help...
After a long period of trial and error I found the solution to my problem. I will be using the contacts lookup key to store the contact and the phone id to store the phone number...as follows:
String selection = ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY + " = '" + lookupkey+ "' and "+Phone._ID+"='"+phoneid+"'";
String[] projection =new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};
Cursor managedCursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, Phone.DISPLAY_NAME + " ASC");
I'm a bit new to all this stuff. As I understand it, an id match is fast, but may not be stable. If a key+id match fails, you should also try a slower match using only the key - Jeffrey Scofield's SQL selection string could be changed to try id-and-key OR key-only (trusting the query optimiser to prioritize the id match).
I haven't had much luck finding information concerning the wisdom of storing the key long term.

Android Cursor select parameter for SMS DB querying

I am trying to get all SMSes from and to a single address.
So while using a Cursor to scan the DB, I tried to use select. But, no values were returned. I suspect that this was caused because the numbers in the "address" colum in the DB were containing space and/or hyphens (-).
My question is that can I make a query to ignore spaces and hyphens in the string?
P.S.: the address I have has been trimmed down to not include spaces and hyphens, and that is unchangeable as per my requirements.
I came across the same situation and tried below method.(not yet sure,was it a proper solution or not! :) )
So,
Query all conversations from conversation table
take a loop and go through all results.For each:
take the address field and remove unwanted character to make it like the number format you have
compare with number you want conversation for : if found => break
you will have the thread-id for the number you are searching sms of,at the end of loop.
Now,
now collect message id of all messages under that thread seperated by type:"sent" and "inbox"
Then you can use those array of id to gather messages from "sent" and "inbox" table of ContentProvider.
I know,the process is bit longer but gave me satisfactory result. :) Hope it helps.

Unable to read from content://sms/all

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.)

Android: How to link sms draft with information about the receiver (like the name)?

if I write a new text message in the Android standard sms app and fill in a receiver and some body text and press the back button Android will save this as a draft. If the receiver is currently in my address book Android tells me also in the draft overview the name or, if the receiver is not in the address book, the number.
So far, so good.
Now I tried to do exactly the same thing on my own and I'm not quite sure why my solution doesn't work.
I've tried the following:
// in a Activity
final ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse("content://sms/draft"),
String[] {"_id", "date", "body", "person"},
null, null, null);
Later in my own cursor-Adapter I tried to get the person-id which links to the contacts table (I thought so). I do it this way:
// in my own CursorAdapter (bindView-Method)
Long receiverId = cursor.getLong("person");
But what I get is always "0". So what do I do wrong? Thanks in advance.
Btw: Is there any official documentation about the "content://sms"-thing? I only found some forum postings but nothing at developers.android.com!?

Categories

Resources