Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android programatically inserted SMS have incorrect timestamp in Messaging apps

Tags:

android

I have tried to use the undocumented content provider (content://sms) to insert a SMS into the inbox, and the insertion is successful. Then I check the newly inserted mesage in the Messaging apps, however the time displayed is always the real time that the insertion is done, instead of the time I specified in ContentValues. After I clicked and viewed the message thread, the time is then updated to the value I set in ContentValues. Have I missed something? Please help, thanks so much

Her is part of my code

Uri uri = Uri.parse("content://sms");
ContentValues cv = new ContentValues();
cv.put("address", "99912345");
cv.put("date", 1309632433677);
cv.put("read", 1);
cv.put("type", 1);
cv.put("subject", null);
cv.put("body", "Testing message");
getContentResolver().insert(uri, cv);
like image 533
Mason Fong Avatar asked Nov 28 '22 10:11

Mason Fong


1 Answers

The problem is that this is an undocumented, unofficial API, so in theory, you maybe shouldn't be using it. In practice, if you want to use it, you have to be prepared for Google to break your software in the J or some later release.

All that said, it turns out that there's a workaround. When you insert a message, that sets the thread's datestamp to the time of insert. But when you delete a message from a thread (identified by the "address" field in the "content://sms" provider), it has to recalculate the thread datestamp. So for every thread you stuff something into, stuff another dummy message in as well and then delete it. Which is easy because the insert method returns a Uri that you can call the delete method on. I suspect this is horribly inefficient.

like image 160
Tim Bray Avatar answered Dec 06 '22 12:12

Tim Bray