17

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;
fvrghl
  • 3,642
  • 5
  • 28
  • 36
rribas
  • 415
  • 1
  • 6
  • 6
  • Can you send a message to the same number from that phone manually? Does adding a country prefix number help? For example, country prefix number for my country is +381. – avra Sep 17 '13 at 09:00

4 Answers4

13

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;
TLama
  • 75,147
  • 17
  • 214
  • 392
mh taqia
  • 3,506
  • 1
  • 24
  • 35
2

The second parameter to sendTextMessage is not the "sender" number, rather it identifies the SMS provider service center.

You almost certainly did not want to specify anything here. Simply pass nil and the SMSManager will use the device default service center for delivering your message.

sRecipient := StringToJString(edRecipient.Text);
sMessage   := StringToJString(edMessage.Text);
sendTextMessage(sRecipient, nil, sMessage, nil, nil);
Deltics
  • 22,162
  • 2
  • 42
  • 70
1

See also:

http://delphi-android.blogspot.dk/2013/10/how-to-send-sms-with-delphi-on-android.html

for a copy & paste function.

I like to have such functions in a separate unit, instead of putting it into the Button's event handler.

Lars D
  • 8,483
  • 7
  • 34
  • 37
1

You can also do it with JIntend object as below

procedure CreateSms(const Number, Msg: string);
var
  Intent: JIntent;
  Uri: Jnet_Uri;
begin
  Uri := TJnet_Uri.JavaClass.parse(StringToJString(Format('smsto:%s', [Number])));
  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, Uri);
  Intent.putExtra(StringToJString('sms_body'), StringToJString(Msg));
  SharedActivity.startActivity(Intent);
end;