How to send SMS with Delphi XE5 in Android

rribas picture rribas · Sep 17, 2013 · Viewed 11.2k times · Source

Does anybody know how to get this to work? The closest I got was the code below, but got no success. At first, it gives you some hope when it tells you need the SEND_SMS permission. But after you setup this permission, nothing happens!

uses
 Androidapi.JNI.JavaTypes;

procedure TForm1.Button1Click(Sender: TObject);
var
  smsManager: JSmsManager;
  smsTo, smsFrom: JString;
begin
  smsManager:= TJSmsManager.JavaClass.getDefault;
  smsTo:= StringToJString('552199999999'); //replace with the right destination number
  smsFrom:= StringToJString('552499999999'); //replace with the right originator number
  smsManager.sendTextMessage(smsTo, smsFrom, StringToJString(Edit1.Text), nil, nil);
end;

Answer

mh taqia picture mh taqia · Sep 17, 2013

Try to pass empty value (nil) to the scAddress parameter of the sendTextMessage function call to use the current default SMS center:

uses
  Androidapi.JNI.JavaTypes, Androidapi.JNI.Telephony;

procedure TForm1.Button1Click(Sender: TObject);
var
  smsTo: JString;
  smsManager: JSmsManager;
begin
  smsManager := TJSmsManager.JavaClass.getDefault;
  smsTo := StringToJString('091...');
  smsManager.sendTextMessage(smsTo, nil, StringToJString('Test SMS'), nil, nil);
end;