如何处理 webView URL 上的 intent://? [英] How to handle intent:// on a webView URL?
问题描述
我目前正在开发一个带有 webView
的 android 应用程序.在某些 urls
上,我需要使用 shouldOverrideUrlLoading
方法,该方法非常简单:
I'm currently developing an android app with a webView
. On some urls
I need to use the shouldOverrideUrlLoading
method, which is pretty straight forward :
if(url.startsWith("something")){
//do something
return true;
}
我的问题:
某些 URLS
,例如在 google 搜索中,具有以下 URL
方案:
Some URLS
, on google search for example, have the following URL
scheme :
intent://en.m.wikipedia.org/wiki/Test_cricket#Intent;scheme=http;package=org.wikipedia;S.browser_fallback_url=https%3A%2F%2Fwww.google.pt%2Fsearchurl%2Fr.html%23app%3Dorg.wikipedia%26pingbase%3Dhttps%3A%2F%2Fwww.google.pt%2F%26url%3Dhttps%3A%2F%2Fen.m.wikipedia.org%2Fwiki%2FTest_cricket;S.android.intent.extra.REFERRER_NAME=https%3A%2F%2Fwww.google.pt;launchFlags=0x8080000;end
我试过使用:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
但我有一个错误:
10-15 15:18:15.546: W/System.err(29086): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=intent://en.m.wikipedia.org/wiki/Test_cricket }
我应该如何处理这种 URL
方案?
How should I handle this kind of URL
scheme ?
更新:
根据@CommonsWare 的评论,我尝试在 Intent
上使用 parseUri()
从 URL 创建一个
Intent 对象
然后尝试 startActivity()
,类似于:
Following @CommonsWare comments, I've tried using parseUri()
on Intent
to create an Intent object
from the URL
and then try startActivity()
, something like:
Intent myIntent = new Intent().parseUri(url, Intent.URI_INTENT_SCHEME);
startActivity(myIntent);
但我仍然得到:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://en.m.wikipedia.org/wiki/Test_cricket flg=0x8080000 pkg=org.wikipedia (has extras) }
我的指令可能有误,或者我根本无法按照@CommonsWare 指定的方式构建intent
.关于如何构建 Intent
的任何提示?
I may have got the instructions wrong or I'm simply unable to construct the intent
as @CommonsWare specified. Any tips on how to construct the Intent
?
推荐答案
这里描述了这个 Intent url 的结构 https://developer.chrome.com/multidevice/android/intents.
Structure of this intent url is described here https://developer.chrome.com/multidevice/android/intents.
所以我们可以像这样在内部 WebView 中处理它:
So we can handle it in internal WebView like this:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("intent://")) {
try {
Context context = view.getContext();
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent != null) {
view.stopLoading();
PackageManager packageManager = context.getPackageManager();
ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (info != null) {
context.startActivity(intent);
} else {
String fallbackUrl = intent.getStringExtra("browser_fallback_url");
view.loadUrl(fallbackUrl);
// or call external broswer
// Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fallbackUrl));
// context.startActivity(browserIntent);
}
return true;
}
} catch (URISyntaxException e) {
if (GeneralData.DEBUG) {
Log.e(TAG, "Can't resolve intent://", e);
}
}
}
return false;
}
这篇关于如何处理 webView URL 上的 intent://?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!