Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 236
user2791639 Avatar asked Sep 18 '13 13:09

user2791639


People also ask

Where is default app settings?

Find and tap Settings → Apps & notifications → Default apps. Tap the type of app you want to set, and then tap the app you want to use as default.

How do I change the default Open Link?

From the Default app menu screen, tap Opening links at the very bottom. Under Installed apps, select the app you want to change the behavior for. Tap (or toggle on) Open supported links.


2 Answers

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;
like image 146
RRUZ Avatar answered Oct 16 '22 10:10

RRUZ


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;
like image 26
blong Avatar answered Oct 16 '22 11:10

blong