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?
Asked
Active
Viewed 8,275 times
10

Rob Kennedy
- 161,384
- 21
- 275
- 467

user2791639
- 103
- 1
- 4
2 Answers
17
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;

RRUZ
- 134,889
- 20
- 356
- 483
-
1Are there any wrappers for this that work on all platforms? For simple default actions shouldn't be too difficult to write one, but I wonder if there is any existing effort to create it. – Leonardo Herrera Sep 18 '13 at 16:57
-
@LeonardoHerrera If there is such a thing, it's not part of FMX. – David Heffernan Sep 18 '13 at 18:29
-
@LeonardoHerrera: It wouldn't be difficult. I don't do iOS or OSX, but I have a version of both of these that now works on Android, Win32, and Win64; based on Rodrigo's code here to start, it took about 10 minutes to write and another 20 to test. – Ken White Sep 19 '13 at 00:02
-
@RRUZ I use these codes but in android when call these functions app crash – Amin Sep 23 '14 at 10:41
-
If u use XE6 to up use Androidapi.Helpers instead of FMX.Helpers.Android – Jessé Catrinck Apr 20 '16 at 20:35
1
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;

blong
- 2,145
- 13
- 23