gpt4 book ai didi

Android 开放 Intent ://protocol in WebView

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:36 28 4
gpt4 key购买 nike

当我将 url 加载到 WebView 并尝试打开链接时,其中一些会显示如下错误页面:

net::ERR_UNKNOWN_URL_SCHEME intent://maps.yandex.ru?utm_medium=tab...

我怎样才能打开这个链接,或者至少检查是否安装了合适的应用程序并运行它?

我试图通过实现自定义 WebViewClient 来覆盖 url 加载:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.startsWith("http")) {
getActivity().startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)));
return true;
}
return false;
}

但在这种情况下,应用会因 ActivityNotFoundException 而崩溃。

02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=intent://maps.yandex.ru?utm_medium=tab-maps&text=магазин&utm_source=serp&yandexuid=2258091361456330110 }
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.Activity.startActivityForResult(Activity.java:3930)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.Activity.startActivityForResult(Activity.java:3890)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.Activity.startActivity(Activity.java:4213)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.Activity.startActivity(Activity.java:4181)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at ru.uxapps.voicesearch.FWeb$CustomWebViewClient.shouldOverrideUrlLoading(FWeb.java:70)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at com.android.webview.chromium.WebViewContentsClientAdapter.shouldOverrideUrlLoading(WebViewContentsClientAdapter.java:325)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:266)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:37)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.os.Looper.loop(Looper.java:148)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
02-24 20:58:51.887 31255-31255/ru.uxapps.voicesearch W/System.err: at java.lang.reflect.Method.invoke(Native Method)

即使安装了所需的应用程序,应用程序也会崩溃。但是如果在 Chrome 中打开此链接,它会显示 maps.yandex.ru 站点,或者打开应用程序(如果已安装)。

是否有一个通用解决方案,适用于所有 intent://链接?感谢您的建议。

最佳答案

我找到了一个解决方案。 This questionthis documentation帮助我了解情况。

因此,我编写了一个遵循以下逻辑的链接处理程序:

  1. 在同一个WebView中打开http和https
  2. 尝试处理已知方案(电话:等)
  3. 如果找不到已知 Activity ,则解析 intent: scheme 并尝试运行适当的应用
  4. 如果未安装所需的应用程序,尝试将提供的后备 url 加载到当前 WebView 中
  5. 如果没有提供后备 url,则重定向到市场并要求安装所需的应用

代码是:

mWebView.setWebViewClient(new CustomWebViewClient());

//...

private class CustomWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("http")) return false;//open web links as usual
//try to find browse activity to handle uri
Uri parsedUri = Uri.parse(url);
PackageManager packageManager = getActivity().getPackageManager();
Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
if (browseIntent.resolveActivity(packageManager) != null) {
getActivity().startActivity(browseIntent);
return true;
}
//if not activity found, try to parse intent://
if (url.startsWith("intent:")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
getActivity().startActivity(intent);
return true;
}
//try to find fallback url
String fallbackUrl = intent.getStringExtra("browser_fallback_url");
if (fallbackUrl != null) {
webView.loadUrl(fallbackUrl);
return true;
}
//invite to install
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(
Uri.parse("market://details?id=" + intent.getPackage()));
if (marketIntent.resolveActivity(packageManager) != null) {
getActivity().startActivity(marketIntent);
return true;
}
} catch (URISyntaxException e) {
//not an intent uri
}
}
return true;//do nothing in other cases
}

}

也许它需要一些清理,但它会有所帮助。如果您知道更简单的方法,请告诉我,我仍在寻找最佳解决方案。

关于Android 开放 Intent ://protocol in WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609107/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com