Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiFiremonkey

Send And Fetch SMS Messages With Delphi Firemonkey On Android

Send SMS Delphi AndroidThe Delphi Android programming blog has posted two threads, with code snippets, regarding SMS messaging with Delphi and Android. The first is How to send SMS with Delphi on Android The second is How to fetch SMS messages from the Android inbox using Delphi.

Send an SMS
uses
FMX.Helpers.Android,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Telephony;

procedure SendSMS (target,messagestr:string);
var
smsManager: JSmsManager;
smsTo: JString;
begin
smsManager:= TJSmsManager.JavaClass.getDefault;
smsTo:= StringToJString(target);
smsManager.sendTextMessage(smsTo, nil, StringToJString(messagestr), nil, nil);
end;

Fetch SMS Messages
uses
SysUtils,
FMX.Helpers.Android,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Telephony;

function FetchSms:string;
var
cursor: JCursor;
uri: Jnet_Uri;
address,person,msgdatesent,protocol,msgread,msgstatus,msgtype,
msgreplypathpresent,subject,body,
servicecenter,locked:string;
msgunixtimestampms:int64;
addressidx,personidx,msgdateidx,msgdatesentidx,protocolidx,msgreadidx,
msgstatusidx,msgtypeidx,msgreplypathpresentidx,subjectidx,bodyidx,
servicecenteridx,lockedidx:integer;
begin
uri:=StrToJURI('content://sms/inbox');
cursor := SharedActivity.getContentResolver.query(uri, nil, nil,nil,nil);
addressidx:=cursor.getColumnIndex(StringToJstring('address'));
personidx:=cursor.getColumnIndex(StringToJstring('person'));
msgdateidx:=cursor.getColumnIndex(StringToJstring('date'));
msgdatesentidx:=cursor.getColumnIndex(StringToJstring('date_sent'));
protocolidx:=cursor.getColumnIndex(StringToJstring('protocol'));
msgreadidx:=cursor.getColumnIndex(StringToJstring('read'));
msgstatusidx:=cursor.getColumnIndex(StringToJstring('status'));
msgtypeidx:=cursor.getColumnIndex(StringToJstring('type'));
msgreplypathpresentidx:=cursor.getColumnIndex(StringToJstring('reply_path_present'));
subjectidx:=cursor.getColumnIndex(StringToJstring('subject'));
bodyidx:=cursor.getColumnIndex(StringToJstring('body'));
servicecenteridx:=cursor.getColumnIndex(StringToJstring('service_center'));
lockedidx:=cursor.getColumnIndex(StringToJstring('locked'));
while (cursor.moveToNext) do begin
address:=JStringToString(cursor.getString(addressidx));
person:=JStringToString(cursor.getString(personidx));
msgunixtimestampms:=cursor.getLong(msgdateidx);
msgdatesent:=JStringToString(cursor.getString(msgdatesentidx));
protocol:=JStringToString(cursor.getString(protocolidx));
msgread:=JStringToString(cursor.getString(msgreadidx));
msgstatus:=JStringToString(cursor.getString(msgstatusidx));
msgtype:=JStringToString(cursor.getString(msgtypeidx));
msgreplypathpresent:=JStringToString(cursor.getString(msgreplypathpresentidx));
subject:=JStringToString(cursor.getString(subjectidx));
body:=JStringToString(cursor.getString(bodyidx));
servicecenter:=JStringToString(cursor.getString(servicecenteridx));
locked:=JStringToString(cursor.getString(lockedidx));
Result:=IntToStr(trunc(msgunixtimestampms/1000))+' '+address+' '+body;
end;
end;

Visit the Delphi Android programming blog and learn How to send SMS messages  and fetch SMS messages using Delphi on Android.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiWindows

Give AI Coding Assistants A Memory That Survives Every Session

DelphiDemoFiremonkeyLibraryShowcaseWindows

Run Large Language Models Natively In Object Pascal

Code SnippetDelphiDemoLibraryShowcaseWindows

Windows CLI To Download, Manage, And Chat With Local AI Models

DelphiDemoOSXWindows

Download, Run, And Chat With Local LLMs On Windows And macOS

Sign up for our Newsletter and
stay informed
[mailpoet_form id="1"]

13 Comments

Leave a Reply