How to send SMS with Delphi XE5 in Android

11,723

Solution 1

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;

Solution 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);

Solution 3

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.

Solution 4

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;
Share:
11,723

Related videos on Youtube

rribas
Author by

rribas

Updated on January 12, 2020

Comments

  • rribas
    rribas over 4 years

    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;
    
    • avra
      avra over 10 years
      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.
  • Machado
    Machado over 8 years
    Format 'smsto:%s' is invalid or incompatible with argument.
  • Sebastian Xawery Wiśniowiecki
    Sebastian Xawery Wiśniowiecki over 8 years
    I have change technology a over year ago after just few months in Delphi and I can not help you now :/
  • Machado
    Machado over 8 years
    There's no problem, I appreciate your concern. However, this seems not to be working anymore.