Does anyone know why I always get date_sent parameter as "0", instead of the correct info when the message was sent? (I get 0 for either sent or received messages, but as far as I remember, both, sent and received, have separate time in stock app, so there surely is that info available, just don't understand why I can't get it. The query used for this is "date_sent"... It's suggested on many sites, but always returns zero.
Are there any newer info about android api accessing messages? I found several info, but all are quite old, and says that messaging is not documented. Nothing changed ever since? Is this https://stackoverflow.com/a/6446831/1786516 all that there is about mms, or is there any delphi xe5 example already made?
UPDATE:
Here's the origin example code for reading smses in Delphi XE5, as provided at several pages:
uses
SysUtils,
FMX.Helpers.Android,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Telephony;
function ReadSMSInbox:string;
var
cursor: JCursor;
uri: Jnet_Uri;
address,person,msgdatesent,protocol,msgread,msgstatus,msgtype,
msgreplypathpresent,subject,body,
servicecenter,locked:string;
msgunixtimestampms:int64;
begin
uri:=StrToJURI('content://sms/inbox');
cursor := SharedActivity.getContentResolver.query(uri, nil, nil,nil,nil);
while (cursor.moveToNext) do begin
address:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('address'))));
person:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('person'))));
msgunixtimestampms:=cursor.getLong(cursor.getColumnIndex(StringToJstring('date')));
msgdatesent:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('date_sent'))));
protocol:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('protocol'))));
msgread:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('read'))));
msgstatus:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('status'))));
msgtype:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('type'))));
msgreplypathpresent:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('reply_path_present'))));
subject:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('subject'))));
body:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('body'))));
servicecenter:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('service_center'))));
locked:=JStringToString(cursor.getString(cursor.getColumnIndex(StringToJstring('locked'))));
Result:=IntToStr(trunc(msgunixtimestampms/1000))+' '+address+' '+body; //+whatever you want to add here;
StringList1.Add(Result); //Add the result in stringlist to collect all of them
end;
end;
There is completely the same code for reading Outbox (Sent) sms messages, except that the function name is ReadSMSOutbox
, and the uri string is uri:=StrToJURI('content://sms/sent');
See the answer for modified/optimized code, and solution about date_sent corrected queries.