【发布时间】:2017-02-22 09:20:03
【问题描述】:
虽然这个问题已经被问过好几次了,但与我的问题真正相关的唯一问题是这个 (Can I use shouldInterceptRequest to block a particular call in Android?),但它在 3 年多前被问到,最终没有得到答复。
我试图阻止的请求是一个 POST 请求,因此 shouldOverrideUrlLoading 方法无法拦截它。
单击按钮后,webview 会加载包含以下字符串的 URL:“androidBundleId_********”。
我需要阻止此请求并将用户重定向到 Play 商店(但我知道该怎么做!)
问题是 shouldInterceptRequest 方法的返回类型是 WebResourceResponse (哦,我多么希望它是布尔值!)。因此,我必须至少返回一个空白的 WebResourceResponse。
我不想这样做,因为 WebResourceResponse 中的所有内容都会加载到我的应用程序中。我只想让 webview 拦截请求并阻止它。
应用应保持显示相同的网页,不应加载任何数据,除了 Play Store Intent。
我只发布我的代码的相关部分。
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if(url.contains("androidBundleId_")){
String bundleID = url.substring(url.indexOf("androidBundleId_")+"androidBundleId".length()+1, url.length());
try {
getContext().startActivity(new Intent
(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + bundleID)));
} catch (android.content.ActivityNotFoundException anfe) {
getContext().startActivity(new Intent
(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + bundleID)));
}
// I'D LIKE TO RETURN NOTHING HERE !!!
}
return super.shouldInterceptRequest(view, url);
}
我知道这个签名已被弃用,但我正在使用 minSdk 16 构建一个应用程序,因此我只能使用这个签名而不是接受 Request 作为第二个参数的那个。
你有什么解决方法吗?请注意,我无法更改包含该按钮的网页。
【问题讨论】:
标签: android webview android-webview