【发布时间】:2016-01-14 01:42:42
【问题描述】:
我目前正在开发一个带有webView 的安卓应用程序。在某些urls 上,我需要使用shouldOverrideUrlLoading 方法,这非常简单:
if(url.startsWith("something")){
//do something
return true;
}
我的问题:
一些URLS,例如在谷歌搜索,有以下URL方案:
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方案?
更新:
在@CommonsWare cmets 之后,我尝试在Intent 上使用parseUri() 从URL 创建一个Intent object,然后尝试startActivity(),类似于:
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 的任何提示?
【问题讨论】:
-
在调用
startActivity()之前,要么捕获ActivityNotFoundException,要么使用PackageManager和resolveActivity()检测这种情况。在这种情况下,Uri指向应用程序(维基百科),如果未安装该应用程序,Intent将无法解析。您需要向用户显示某种错误消息,表明这一事实。 -
@CommonsWare 如果点击带有
intent://...的链接,某些浏览器(Firefox、Chrome、Opera)会成功打开应用程序,有没有办法将 uri 转发到默认浏览器并让它处理它? -
默认浏览器没有处理它——维基百科应用程序应该处理它。不过,现在我想起来了,
ACTION_VIEW可能不是正确的答案。在Intent上使用parseUri()从URL 创建一个Intent对象,然后在该Intent上尝试startActivity()。 -
@CommonsWare,有道理,让我试一试。之后我会告诉你的。
-
@PedroLobito:尝试将
BROWSABLE类别添加到您正在创建的意图对象中——这是浏览器实际所做的:myIntent.addCategory(Intent.CATEGORY_BROWSABLE);看起来维基百科应用程序在其意图过滤器中指定了此类别:@ 987654321@