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;
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;
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);
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.
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;
Related
How I can open Short Message Service (SMS) window on Android phode with pretyped message text and recipient number.
In manuals i found only Phone dialer
PhoneDialerService.Call(edtTelephoneNumber.Text)
which allow make call, but nothing about sending SMS messages.
See the class SMSManager on Android API.
There is a sendTextMessage method that you can use:
Try some code like this:
uses
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Telephony;
procedure TForm1.Button1Click(Sender: TObject);
var
destAdress: JString;
smsManager: JSmsManager;
begin
smsManager := TJSmsManager.JavaClass.getDefault;
destAdress := StringToJString('0034123456789');
smsManager.sendTextMessage(destAdress, nil, StringToJString('The message content'), nil, nil);
end;
You must add permissions to the project configuration. See the "Send SMS":
Send SMS - Allows an application to send SMS messages.
Regards.
The question asks about launching the SMS window, which I've taken to mean launching the built-in SMS-sending app. It wants a target number and message content sent in.
This can be done by asking Android to start an activity using an intent set up to view a carefully crafted SMS-specific URI and containing the required message as an extra field.
The primary benefit to invoking the SMS app rather than sending the SMS within your own app is that it does not require additional permissions - the SMS app already has those permissions.
I show how to do this in my old activity launching article (which was written for Delphi XE6 but is still applicable to all later versions, with a few minor code updates here and there), but for completeness here is a helper unit that does the job:
unit SMSHelperU;
interface
procedure CreateSMS(const Number, Msg: string);
implementation
uses
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.App,
Androidapi.JNI.Net,
Androidapi.JNI.GraphicsContentViewText,
System.SysUtils;
function LaunchActivity(const Intent: JIntent): Boolean; overload;
var
ResolveInfo: JResolveInfo;
begin
{$IF RTLVersion >= 30}
ResolveInfo := TAndroidHelper.Activity.getPackageManager.resolveActivity(Intent, 0);
{$ELSE}
ResolveInfo := SharedActivity.getPackageManager.resolveActivity(Intent, 0);
{$ENDIF}
Result := ResolveInfo <> nil;
if Result then
{$IF RTLVersion >= 30}
TAndroidHelper.Activity.startActivity(Intent);
{$ELSE}
SharedActivity.startActivity(Intent);
{$ENDIF}
end;
procedure CreateSMS(const Number, Msg: string);
var
Intent: JIntent;
URI: Jnet_Uri;
begin
URI := StrToJURI(Format('smsto:%s', [Number]));
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, URI);
Intent.putExtra(StringToJString('sms_body'), StringToJString(Msg));
LaunchActivity(Intent);
end;
end.
The routine can be called like this:
CreateSMS('123456789', 'O HAI2U!');
If you did want to actually send the SMS the unit below does that job but also includes code that self-checks the relevant permission is set for the app:
unit AndroidStuff;
interface
function HasPermission(const Permission: string): Boolean;
procedure SendSMS(const Number, Msg: string);
implementation
uses
System.SysUtils,
System.UITypes,
{$IF RTLVersion >= 31}
FMX.DialogService,
{$ELSE}
FMX.Dialogs,
{$ENDIF}
FMX.Helpers.Android,
Androidapi.Helpers,
Androidapi.JNI.App,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Telephony;
function HasPermission(const Permission: string): Boolean;
begin
//Permissions listed at http://d.android.com/reference/android/Manifest.permission.html
{$IF RTLVersion >= 30}
Result := TAndroidHelper.Context.checkCallingOrSelfPermission(
{$ELSE}
Result := SharedActivityContext.checkCallingOrSelfPermission(
{$ENDIF}
StringToJString(Permission)) =
TJPackageManager.JavaClass.PERMISSION_GRANTED
end;
procedure SendSMS(const Number, Msg: string);
var
SmsManager: JSmsManager;
begin
if not HasPermission('android.permission.SEND_SMS') then
{$IF RTLVersion >= 31}
TDialogService.MessageDialog('App does not have the SEND_SMS permission', TMsgDlgType.mtError, [TMsgDlgBtn.mbCancel], TMsgDlgBtn.mbCancel, 0, nil)
{$ELSE}
MessageDlg('App does not have the SEND_SMS permission', TMsgDlgType.mtError, [TMsgDlgBtn.mbCancel], 0)
{$ENDIF}
else
begin
SmsManager := TJSmsManager.JavaClass.getDefault;
SmsManager.sendTextMessage(
StringToJString(Number),
nil,
StringToJString(Msg),
nil,
nil);
end;
end;
end.
I need get telephone number of device on which my application runing. If has device two SIM cards ideal to get both numbers or if SIM card is not inserted (tablet device) can detect this.
I found some JAVA code but I have no idea how translate it to Delphi
TelephonyManager phneMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String phneNmbr = phneMgr.getLine1Number();
I try write something but it not working ....
USES Androidapi.Helpers, Androidapi.JNI.JavaTypes, Androidapi.JNI.Telephony;
procedure TForm1.Button1Click(Sender: TObject);
var
num: JString;
tman: Androidapi.JNI.Telephony.JTelephonyManager;
begin
tman:=TJtelephonyManager.Create;
num := tman.getLine1Number;
edit1.Text:=Jstringtostring(num);
end;
Something like this should do it, based on experience with other system services. This translates what you have suggested is viable Java code.
I'll edit this to make it compile correctly (if there are any issues with it) when I have a copy of Delphi to hand later, but this is roughly what is required.
Note that quick look at the telephony manager documentation doesn't readily say how one would get the phone number for a second SIM, but it does translate what you were trying to translate.
uses
System.SysUtils,
Androidapi.Helpers,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Telephony;
function DeviceTelephoneNumber: string;
var
TelephonyManagerObj: JObject;
TelephonyManager: JTelephonyManager;
begin
TelephonyManagerObj:= TAndroidHelper.Context.getSystemService(
TJContext.JavaClass.TELEPHONY_SERVICE);
if TelephonyManagerObj <> nil then
begin
TelephonyManager := TJTelephonyManager.Wrap(TelephonyManagerObj);
if TelephonyManager <> nil then
Result := JStringToString(TelephonyManager.getLine1Number);
end;
end;
This code is also a possibility, which works in Android 5.1 and later.
function DeviceTelephoneNumbers: TArray<string>;
var
SubscriptionManager: JSubscriptionManager;
I, SubscriptionInfoCount: Integer;
SubscriptionInfoList: JList;
SubscriptionInfo: JSubscriptionInfo;
begin
// Subscription manager is only available in Android 5.1 and later
if TOSVersion.Check(5, 1) then
begin
SubscriptionManager := TJSubscriptionManager.JavaClass.from(
TAndroidHelper.Context);
SubscriptionInfoCount := SubscriptionManager.getActiveSubscriptionInfoCount;
SubscriptionInfoList := SubscriptionManager.getActiveSubscriptionInfoList;
SetLength(Result, SubscriptionInfoCount);
for I := 0 to Pred(SubscriptionInfoCount) do
begin
SubscriptionInfo := TJSubscriptionInfo.Wrap(SubscriptionInfoList.get(I));
if SubscriptionInfo <> nil then
Result[I] := JStringToString(SubscriptionInfo.getNumber);
end;
end
else
begin
// If running on older OS, use older API
SetLength(Result, SubscriptionInfoCount);
Result[0] := DeviceTelephoneNumber
end;
end;
I use Delphi 10 Seattle update1 and I have an android service what I start from host app but I do not know how can I stop the service from the host application. Could anyone tell me, please?
You are starting the service using the
TLocalServiceConnection.StartService() method. Embarcadero does not provide a corresponding TLocalServiceConnection.StopService() method, so you will have to call Android's Context.stopService() method directly.
Here is the source code for TLocalServiceConnection.startService() from $(BDS)\source\rtl\android\System.Android.Service.pas:
class procedure TLocalServiceConnection.StartService(const AServiceName: string);
var
LIntent: JIntent;
LService: string;
begin
LIntent := TJIntent.Create;
LService := AServiceName;
if not LService.StartsWith('com.embarcadero.services.') then
LService := 'com.embarcadero.services.' + LService;
LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString(LService));
TAndroidHelper.Activity.startService(LIntent);
end;
You can replace TAndroidHelper.Activity.startService() with TAndroidHelper.Activity.stopService():
var
LIntent: JIntent;
begin
LIntent := TJIntent.Create;
LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString('com.embarcadero.services.LocationService'));
TAndroidHelper.Activity.stopService(LIntent);
end;
I just made an app with Delphi XE6 that receives push notifications with kinvey based on this example
When the application is running and I send a push the PushEvent handler receives it well, but when the application is closed and I press the notification it only opens my app.
Can I know which notification was pressed and get parameters from it?
Thanks in advance.
Edit:
I get a little bit closer, in my FormCreate ask for Extras:
procedure TForm1.FormCreate(Sender: TObject);
var
LIntent: JIntent;
LExtras: JBundle;
LExtrasArray: TJavaObjectArray<AndroidApi.JNI.JavaTypes.JObject>;
begin
LIntent := SharedActivity.getIntent;
try
if LIntent <> nil then
begin
LExtras := LIntent.getExtras;
if LExtras <> nil then
begin
//Now try to get the data
LExtrasArray := LExtras.KeySet.toArray;
for I := 0 to LExtrasArray.Length - 1 do
Memo1.Lines.Add(JStringToString(LExtrasArray.Items[I].toString));
end;
end;
finally
LIntent := nil;
end;
end;
With this code I get "gcm" in my memo.
So, when the notification fires my app I get this Extra available.
Now the problem is how I get info about that extra?
I tried LExtras.getString(StringToJString('message')) but this writes '' instead of the push message
Sarina DuPont answer me in her Blog
PushEvents component has a property StartupNotification for this purpose
procedure TMainForm.FormShow(Sender: TObject);
begin
if Assigned(PushEvents.StartupNotification) then
//Do something here!
//for example
//Memo.Text := PushEvents.StartupNotification.Message;
end;
I have managed to register a custom protocol handler in my XE5 Android app by modifying the Androidmanifest.template.xml file. My application pops up properly whenever a myapp://myurl URL is clicked on.
The problem is, I need to get the URL that was clicked on when the app is launched in order to bring up the correct part of the app. Can anyone help me figure out how to get this?
Based on this example, try something like this:
uses
...,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,
FMX.Helpers.Android;
procedure TMainForm.FormCreate(Sender: TObject);
var
intent: JIntent;
uri: Jnet_Uri;
uriStr: String;
begin
intent := SharedActivity.getIntent;
if intent <> nil then
begin
if TJIntent.JavaClass.ACTION_VIEW.equals(intent.getAction) then
begin
uri := intent.getData;
uriStr := JStringToString(uri.toString);
// use uriStr as needed...
end;
end;
end;