(Delphi 10.2) 如何从我的应用程序在 Android 的 Web 浏览器中打开 URL?

2024-03-07

Delphi 10.2.2 Firemonkey Android 问题:如何从我的应用程序中打开 Android 网络浏览器中的 URL?

我试图理解这里的概念:如何从我的应用程序在 Android 的网络浏览器中打开 URL? https://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application但这些信息对我没有任何帮助。与我目前拥有的代码相比,我无法理解他们如何让它工作。

我也看过这里:http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/FMX.Android_Intents_Sample http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/FMX.Android_Intents_Sample

我也看过这里:https://developer.android.com/guide/components/intents-common.html?hl=en https://developer.android.com/guide/components/intents-common.html?hl=en

我试过这段代码:

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;

这就是我使用该功能的方式:

OpenURL('https://www.patreon.com/phonelosers/overview/', true)

它一直给我一个错误:

错误:android.content.ActivityNotFoundException:找不到处理 Intent { act=android.intent.action.VIEW dat= 的活动https://www.patreon.com/phonelosers/overview/ https://www.patreon.com/phonelosers/overview/ }

我也尝试过这段代码:

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;

这给了我“Intent.resolveActivity

我使用哪款 Android 手机并不重要。我有一台 Moto G Play、三星 S8+ 和一台 Moto Z2 Force,它们运行 Android 版本 6.0.1、7.0、8.0。所有手机都安装了 Chrome 网络浏览器,我可以使用它。

我浏览了整个网络,下面的代码是每个人都用来做我需要它做的事情的。我查看了Delphi和Android编程资料。

请帮助解决这个 Delphi 10.2.2 Firemonkey Android 问题!


使用不同的编码方法后:

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;

我发现我忘记了“System.NetEncoding.TNetEncoding.URL.Encode”,只需从源代码中删除该代码就可以解决问题,以便此代码:

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;

现在可以了!

从此System.NetEncoding.TNetEncoding.URL.Encode引起了这个问题,我想知道我是否需要对我的网址进行特殊编码,我应该使用什么?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

(Delphi 10.2) 如何从我的应用程序在 Android 的 Web 浏览器中打开 URL? 的相关文章

随机推荐