Solve the problems that Android WebView intercepts url and video playback fails to load
- 2021-11-29 08:28:20
- OfStack
Requirements: When Android calls webView to load a web page, intercept a link without executing this link, and execute the specified jump to other activity pages.
Several apis are provided in the setWebViewClient method of webview:
// This callback is to intercept the click to jump url Link to the requested url Link to make changes (add and delete fields)
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
// Called only after clicking on the requested link. Overriding this method returns true Indicates that clicking the link in the webpage is still in the current webview Jump inside, don't jump to the browser. We can do many operations with this function, for example, we read some special URL So you can cancel this operation without opening the address and do other predefined operations, which is right 1 A program is very necessary.
public boolean shouldOverrideUrlLoading(WebView view, String url)
So my requirement is to do it in the shouldOverrideUrlLoading method
webView.setWebViewClient(new WebViewClient() {
@Override
// Called only after clicking on the requested link. Overriding this method returns true Indicates that clicking the link in the webpage is still in the current webview Jump inside, don't jump to the browser. We can do many operations with this function, for example, we read some special URL So you can cancel this operation without opening the address and do other predefined operations, which is right 1 A program is very necessary.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Judge url Whether the link contains a field, and if so, the specified jump is performed (no jump is performed url Link), and load it if it does not url Link
if (url.contains("/mproduct-")) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
return true;
} else {
return false;
}
}
});
Some other settings of webview
// Click the return button to return to 1 Pages instead of exiting the program
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();// Before returning 1 Pages
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Clear webview Cache
webView.clearCache(true);
}
private void initView() {
webView = (WebView) findViewById(R.id.webView);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);// Set up support for scaling
settings.setSupportZoom(false);// Zoom is not supported
settings.setUseWideViewPort(false);// Adjust the picture to fit webview Size
settings.setLoadWithOverviewMode(true);// Zoom to screen size
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);// Support caching
}
Introduction of common methods of WebSettings:
setJavaScriptEnabled(true); // Support js
setPluginsEnabled(true); // Support plug-ins
setUseWideViewPort(false); // Adjust the picture to fit webview The size of
setSupportZoom(true); // Support for Zoom
setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); // Support content relayout
supportMultipleWindows(); // Multi-window
setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // Shut down webview Cache in
setAllowFileAccess(true); // Setting access to files
setNeedInitialFocus(true); // When webview Call requestFocus When is webview Setting Node
webview webSettings.setBuiltInZoomControls(true); // Set up support for scaling
setJavaScriptCanOpenWindowsAutomatically(true); // Support the adoption JS Open a new window
setLoadWithOverviewMode(true); // Zoom to screen size
setLoadsImagesAutomatically(true); // Support automatic loading of pictures
The complete solution of WebViewClient method:
doUpdateVisitedHistory(WebView view, String url, boolean isReload) //( Update history )
onFormResubmission(WebView view, Message dontResend, Message resend) //( The application re-requests web page data )
onLoadResource(WebView view, String url) // Called when the page resource is loaded, every 1 Will call the loading of resources (such as pictures) 1 Times.
onPageStarted(WebView view, String url, Bitmap favicon) // This event is called at the beginning of loading the page. Usually we can set it here 1 A loading That tells the user that the program is waiting for a network response.
onPageFinished(WebView view, String url) // Called at the end of page loading. By the same token, we know that 1 Pages are loaded, so we can close loading Bar, switch program actions.
onReceivedError(WebView view, int errorCode, String description, String failingUrl)// ( Reporting error messages )
onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,String realm)// (Get Return Information Authorization Request)
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) // Overriding this method can make webview Deal with https Request.
onScaleChanged(WebView view, float oldScale, float newScale) // (WebView Called when a change occurs )
onUnhandledKeyEvent(WebView view, KeyEvent event) // ( Key Called when the event is not loaded)
shouldOverrideKeyEvent(WebView view, KeyEvent event)// Override this method to handle key events in the browser.
shouldOverrideUrlLoading(WebView view, String url)
// Called only after clicking on the requested link. Overriding this method returns true Indicates that clicking the link in the webpage is still in the current webview Jump inside, don't jump to the browser. We can do many operations with this function, for example, we read some special URL So you can cancel this operation without opening the address and do other predefined operations, which is right 1 A program is very necessary.
Solution to the problem of video playback loading failure in H5 and H5 loaded by WebView:
1. Add the following attributes to the activity configuration file where webview is located in AndroidManifest. xml
android:hardwareAccelerated="true"
2. Add webSettings. setPluginState (WebSettings. PluginState. ON);
If it is not added, the video will fail to load
3. This judgment should be added to mobile phones above 5.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}