Handling Custom URI in Delphi XE5 Android App - android

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;

Related

Delphi/Firemonkey How to call Settings.System.canWrite(context) (Android)

I'm using Delphi 10.3 Community Edition and want to use the WRITE_SETTINGS in my application to set the brightness.
I could get it managed to implement this procedure to call the settings dialog:
procedure RequestWriteSettings;
var
Intent: JIntent;
begin
Intent := TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_MANAGE_WRITE_SETTINGS);
TAndroidHelper.Activity.startActivity(Intent);
end;
I can call this procedure in my application, the dialog appears and I can set the necessary permissions.
But I don't want to call this procedure permanently, because that's not user friendly.
I need to check if the WRITE_SETTINGS permission is already set, but I don't know how to implement this in Delphi/Firemonkey.
What I could find is that one has to call the "Settings.System.canWrite(context)" function, but I only can find samples for java.
Calling these kind of java routines in Delphi isn't that easy. I'm searching around already for some weeks and tried "things on my own", but still without success.
Can someone provide the code line how this routine has to be called in Delphi?
Thanks so much in advance!
MPage
Example code for checking WRITE_SETTINGS:
uses
Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Provider, Androidapi.JNI.Net, Androidapi.Helpers;
procedure TForm1.RequestWriteSettingsButtonClick(Sender: TObject);
begin
if not TJSettings_System.JavaClass.canWrite(TAndroidHelper.Context) then
StartWritePermissionsActivity
else
ShowMessage('System says app can write settings');
end;
procedure TForm1.StartWritePermissionsActivity;
var
LIntent: JIntent;
begin
LIntent := TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_MANAGE_WRITE_SETTINGS);
LIntent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:').concat(TAndroidHelper.Context.getPackageName)));
TAndroidHelper.Context.startActivity(LIntent);
end;
In the meanwhile I found a solution for myself, but I think Dave's is better. ;-)
That's what I found with the "trial and error" method:
function HasWriteSettings: Boolean;
begin
// Call canWrite to check for permission WRITE_SETTINGS
Result := TJSettings_System.JavaClass.canWrite(TAndroidHelper.Context.getApplicationContext);
end;

(Delphi 10.2) How can I open a URL in Android's web browser from my application?

Delphi 10.2.2 Firemonkey Android Question: How can I open a URL in Android's web browser from my application?
I tried to understand the concepts here: How can I open a URL in Android's web browser from my application? but the information didn't help me any. I am unable to wrap my brain around how they can get it to work compared to the code I currently have.
I've looked here too: http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/FMX.Android_Intents_Sample
I've also looked here: https://developer.android.com/guide/components/intents-common.html?hl=en
I've tried this code:
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(System.NetEncoding.TNetEncoding.URL.Encode(URL))));
try
TAndroidHelper.Activity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
This is how I use that function:
OpenURL('https://www.patreon.com/phonelosers/overview/', true)
And it keeps giving me an error:
Error: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://www.patreon.com/phonelosers/overview/ }
I tried this code too:
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(System.NetEncoding.TNetEncoding.URL.Encode(URL))));
if TAndroidHelper.Activity.getPackageManager.queryIntentActivities(Intent, TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size > 0 then
begin
try
TAndroidHelper.Activity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end
else
begin
ShowMessage('Intent.resolveActivity <= 0');
end;
end;
which gives me "Intent.resolveActivity <= 0"
It doesn't matter which android phone I use. I have a Moto G Play, Samsung S8+, and a Moto Z2 Force which run Android versions 6.0.1, 7.0, 8.0. All the phones have chrome web browser installed and I can use it.
I look all over the web and the code below is what everyone is using to supposedly do what I need it to do. I looked at both Delphi and Android programming information.
Please help solve this Delphi 10.2.2 Firemonkey Android problem!
After using a different coding method:
procedure OpenURL(const URL: string);
var
LIntent: JIntent;
Data: Jnet_Uri;
begin
LIntent := TJIntent.Create;
Data := TJnet_Uri.JavaClass.parse(StringToJString(URL));
LIntent.setData(Data);
LIntent.setAction(StringToJString('android.intent.action.VIEW'));
TAndroidHelper.Activity.startActivity(LIntent);
end;
I figured out I forgot "System.NetEncoding.TNetEncoding.URL.Encode" and just removing that code from the source fixed the issue so that this code:
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
try
TAndroidHelper.Activity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
now works!
Since this System.NetEncoding.TNetEncoding.URL.Encode has caused the issue, I'm wondering if I do need to encode my url special, what should I use?
use this code :
var
URL: string;
Intent: JIntent;
begin
URL := 'https://www.google.com';
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
SharedActivity.startActivity(Intent);
end

Android push notification with actions/parameters

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;

How do I open URLs, PDFs, etc. with the default apps?

I am developing an Android application with Delphi XE5, and I would like to know how I can open a URL in the default browser, and a PDF file with the default reader.
Developing for Windows, I used ShellExecute, but for Android and iOS what should I use?
For these kind pf task you can use the Intent class which is represented in Delphi by the JIntent interface.
Try these samples
Open a URL
uses
Androidapi.JNI.GraphicsContentViewText,
FMX.Helpers.Android;
procedure TForm3.Button1Click(Sender: TObject);
var
Intent: JIntent;
begin
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setData(StrToJURI('http://www.google.com'));
SharedActivity.startActivity(Intent);
end;
Open a PDF File
uses
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
FMX.Helpers.Android;
procedure TForm3.Button1Click(Sender: TObject);
var
Intent: JIntent;
begin
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(StrToJURI('filepath'), StringToJString('application/pdf'));
SharedActivity.startActivity(Intent);
end;
n00b here can't work out how to add a comment to the set of comments already posted against the previous answer, but I use this, which is another variation on the theme, using constructor parameters:
procedure LaunchURL(const URL: string);
var
Intent: JIntent;
begin
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
SharedActivity.startActivity(Intent);
end;

How to send SMS with Delphi XE5 in Android

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;

Categories

Resources