D
D
Denis Demin2019-04-28 18:33:40
Android
Denis Demin, 2019-04-28 18:33:40

Open browser from android app?

So, now about my question. I searched for a long time and found a lot of things that are not clear to me. I'm just learning and I ask you not to kick me hard, but to explain without swearing. We all started learning at some point.
I'm making a simple Android application in Delphi XE 10. I'm
interested in the following: How to open a browser using a specific link through a label or maybe a button?
Is it really such a hard task that no one knows?
If there is an example source code, please share it for analysis and familiarization. For three days now I have not been able to gather my brains together, nothing happens.
Thanks a lot for your help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zed, 2019-04-28
@zedxxx

Try this code ( source ):

unit OpenViewUrl;
 
interface
 
// URLEncode is performed on the URL
// so you need to format it   protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
 
implementation
 
uses
  IdURI, SysUtils, Classes, FMX.Dialogs,
{$IFDEF ANDROID}
  Androidapi.Helpers,
  FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{$ELSE}
{$IFDEF IOS}
  Macapi.Helpers, iOSapi.Foundation, FMX.Helpers.iOS;
{$ENDIF IOS}
{$ENDIF ANDROID}
 
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
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(TIdURI.URLEncode(URL))));
  try
    SharedActivity.startActivity(Intent);
    exit(true);
  except
    on e: Exception do
    begin
      if DisplayError then ShowMessage('Error: ' + e.Message);
      exit(false);
    end;
  end;
end;
{$ELSE}
{$IFDEF IOS}
var
  NSU: NSUrl;
begin
  // iOS doesn't like spaces, so URL encode is important.
  NSU := StrToNSUrl(TIdURI.URLEncode(URL));
  if SharedApplication.canOpenURL(NSU) then
    exit(SharedApplication.openUrl(NSU))
  else
  begin
    if DisplayError then
      ShowMessage('Error: Opening "' + URL + '" not supported.');
    exit(false);
  end;
end;
{$ELSE}
begin
  raise Exception.Create('Not supported!');
end;
{$ENDIF IOS}
{$ENDIF ANDROID}
 
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question