(Delphi 10.2)如何从我的应用程序在Android的网络浏览器中打开URL? [英] (Delphi 10.2) How can I open a URL in Android's web browser from my application?

查看:138
本文介绍了(Delphi 10.2)如何从我的应用程序在Android的网络浏览器中打开URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Delphi 10.2.2 Firemonkey Android Question: How can I open a URL in Android's web browser from my application?

我试图在此处了解概念:如何可以从我的应用程序在Android的网络浏览器中打开URL吗?,但是这些信息对我没有任何帮助。与我目前拥有的代码相比,我无法集中精力了解他们如何使它工作。

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.

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

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

我已经尝试过以下代码:

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:

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

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;

这使我获得 Intent.resolveActivity< = 0

我使用哪个Android手机都没关系。我有一个Moto G Play,三星S8 +和一个运行Android 6.0.1、7.0、8.0版的Moto Z2 Force。所有的电话都安装了chrome浏览器,我可以使用它。

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.

我在整个Web上浏览,下面的代码是每个人用来实现我所需要的功能它要做。我同时查看了Delphi和Android编程信息。

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.

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

Please help solve this Delphi 10.2.2 Firemonkey Android problem!

推荐答案

使用其他编码方法后:

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,只是删除了该代码从源头上解决了该问题,因此该代码如下:

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;

现在可以工作了!

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

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?

这篇关于(Delphi 10.2)如何从我的应用程序在Android的网络浏览器中打开URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆