shouldOverrideUrl 是如何调用的
How is shouldOverrideUrl called
我对 android 开发完全陌生。
最近我被要求调查一些关于我们应用程序的 webview 加载的事情,它是由 flutter 编写的,并使用 flutter_webview_plugin.
升级后 flutter_webview_plugin,我发现有一些变化。
flutter_webview_plugin
中有代码
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Map<String, Object> data = new HashMap<>();
data.put("url", url);
data.put("type", "startLoad");
FlutterWebviewPlugin.channel.invokeMethod("onState", data);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Map<String, Object> data = new HashMap<>();
data.put("url", url);
FlutterWebviewPlugin.channel.invokeMethod("onUrlChanged", data);
data.put("type", "finishLoad");
FlutterWebviewPlugin.channel.invokeMethod("onState", data);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// returning true causes the current WebView to abort loading the URL,
// while returning false causes the WebView to continue loading the URL as usual.
String url = request.getUrl().toString();
boolean isInvalid = checkInvalidUrl(url);
Map<String, Object> data = new HashMap<>();
data.put("url", url);
data.put("type", isInvalid ? "abortLoad" : "shouldStart");
FlutterWebviewPlugin.channel.invokeMethod("onState", data);
return isInvalid;
}
我尝试使用 shouldOverrideUrlLoading, onPageStarted,onPageFinished
到处搜索,但找不到调用它们的位置。
我认为它们应该被喜欢的人使用:
BrowserClient webViewClient;
webviewClient.shouldOverrideUrlLoading()
或
webViewClient.invokeMethod('shouldOverrideUrlLoading',arg)
类似上面的内容。但是我什么也找不到。
当应该加载页面时,WebView 会自行调用这些方法。
假设 WebView 正在尝试加载 www.google.com,方法 shouldOverrideUrlLoading
将调用传递给 request
的网址 www.google.com参数。
您可以 return true
或 false
告诉 WebView 是否应该加载 url,或者是否应该停止它。
一个常见的用例是防止用户在应用内浏览器中离开特定网页。
希望对您有所帮助!!
我对 android 开发完全陌生。 最近我被要求调查一些关于我们应用程序的 webview 加载的事情,它是由 flutter 编写的,并使用 flutter_webview_plugin.
升级后 flutter_webview_plugin,我发现有一些变化。
flutter_webview_plugin
中有代码 @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Map<String, Object> data = new HashMap<>();
data.put("url", url);
data.put("type", "startLoad");
FlutterWebviewPlugin.channel.invokeMethod("onState", data);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Map<String, Object> data = new HashMap<>();
data.put("url", url);
FlutterWebviewPlugin.channel.invokeMethod("onUrlChanged", data);
data.put("type", "finishLoad");
FlutterWebviewPlugin.channel.invokeMethod("onState", data);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// returning true causes the current WebView to abort loading the URL,
// while returning false causes the WebView to continue loading the URL as usual.
String url = request.getUrl().toString();
boolean isInvalid = checkInvalidUrl(url);
Map<String, Object> data = new HashMap<>();
data.put("url", url);
data.put("type", isInvalid ? "abortLoad" : "shouldStart");
FlutterWebviewPlugin.channel.invokeMethod("onState", data);
return isInvalid;
}
我尝试使用 shouldOverrideUrlLoading, onPageStarted,onPageFinished
到处搜索,但找不到调用它们的位置。
我认为它们应该被喜欢的人使用:
BrowserClient webViewClient;
webviewClient.shouldOverrideUrlLoading()
或
webViewClient.invokeMethod('shouldOverrideUrlLoading',arg)
类似上面的内容。但是我什么也找不到。
当应该加载页面时,WebView 会自行调用这些方法。
假设 WebView 正在尝试加载 www.google.com,方法 shouldOverrideUrlLoading
将调用传递给 request
的网址 www.google.com参数。
您可以 return true
或 false
告诉 WebView 是否应该加载 url,或者是否应该停止它。
一个常见的用例是防止用户在应用内浏览器中离开特定网页。
希望对您有所帮助!!