In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use android's WebView. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Use
At first, I believe that we all add WebView controls directly to the layout file, of course, I did the same at the beginning, just for simplicity, and I don't know what's wrong with this.
Directly added a WebView, found that there is no problem ah, it can also show that everything is normal ah. When you repeatedly open a page with WebView, you will find that the memory of the application will continue to rise, it will not come down after it is destroyed, and it will not come down even if you click GC, so there will be a memory leak. At this time, you will find that it is incorrect to use WebView, so how to use it?
That is to add dynamically in the code.
First declare a parent layout in the layout file
Then in the code, add WebView as its child View
WebView webView = new WebView (context); webViewLayout.addView (webView)
Many people on the Internet say that this context should use application, which I don't think is right. What if your WebView needs to pop up a dialog? There are other unpredictable problems, and it is most appropriate to use the current activity Context.
The above is how to add WebView to use, and then what exactly are its properties that we need to use in development?
WebView.loadUrl ("www.baidu.com"); / / Web pages loaded by WebView use loadUrl WebSettings webSettings = webView.getSettings (); / / get the setting of WebView webSettings.setUseWideViewPort (true); / / set this property to scale webSettings.setLoadWithOverviewMode (true) at any scale; / / adapt webSettings.setJavaScriptEnabled (true); / / support js webSettings.setCacheMode (WebSettings.LOAD_DEFAULT); / / set cache mode webSettings.setDomStorageEnabled (true) / / enable the DOM storage API function webSettings.setDatabaseEnabled (true); / / enable the database storage API function webSettings.setMixedContentMode (WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); / / HTTPS, note that this is the webSettings.setAppCacheEnabled (true) only called above LOLLIPOP; / / enable the Application Caches function webSettings.setBlockNetworkImage (true); / / disable loading of network images, which can be set to true at the beginning of loading and false when the web page is loaded.
The above is the use of the most basic settings in WebView, which I believe will be set up during the development process.
WebView.setWebChromeClient (new WebChromeClient () {@ Override public void onProgressChanged (WebView view, int newProgress) {/ / loading progress} @ Override public void onReceivedTitle (WebView view, String title) {/ / get the title of WebView} @ Override public boolean onJsAlert (WebView view, String url, String message) Final JsResult result) {return super.onJsAlert (view, url, message, result) / / Js pop-up box} @ Override public boolean onJsConfirm (WebView view, String url, String message, final JsResult result) {AlertDialog.Builder b = new AlertDialog.Builder (IllegalQueryActivity.this); b.setTitle ("delete"); b.setMessage (message) B.setPositiveButton (android.R.string.ok, new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {result.confirm ();}}) B.setNegativeButton (android.R.string.cancel, new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {result.cancel ();}}); b.create () .show (); return true }}); webView.setWebViewClient (new WebViewClient () {@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {/ / needs to be set to display web pages in the current WebView, so that it will not jump to the default browser to display return true } @ Override public void onReceivedError (WebView view, WebResourceRequest request, WebResourceError error) {super.onReceivedError (view, request, error); / / loading error} @ Override public void onPageFinished (WebView view, String url) {super.onPageFinished (view, url); / / loading completed}}) WebView.setDownloadListener (new DownLoadListener ()); / / download listening private class DownLoadListener implements DownloadListener {@ Override public void onDownloadStart (String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {}}
And then there's the interaction between WebView and JS.
WebView.addJavascriptInterface (new WebAppInterface (this), "WebJs"); public class WebAppInterface {Context mContext; public WebAppInterface (Context c) {mContext = c;} @ JavascriptInterface public void method () {}} webView.loadUrl ("_ javascript:jsMethod ()"); / / this is the easiest way for WebView to call JS
When activity executes the life cycle, it is important to note that when onDestroy, the WebView needs to be destroyed, otherwise memory leaks will also occur.
@ Overrideprotected void onPause () {super.onPause (); if (webView! = null) {webView.onPause ();}} @ Overrideprotected void onResume () {super.onResume (); if (webView! = null) {webView.onResume () } @ Override protected void onDestroy () {if (webView! = null) {webView.clearCache (true) / / clear the cache if (android.os.Build.VERSION.SDK_INT > = android.os.Build.VERSION_CODES.LOLLIPOP) {if (webViewLayout! = null) {webViewLayout.removeView (webView);} webView.removeAllViews () WebView.destroy ();} else {webView.removeAllViews (); webView.destroy (); if (webViewLayout! = null) {webViewLayout.removeView (webView);}} webView = null }}
You can see that the version of the system is judged in the onDestroy method above. That is because I have tested the system in different versions. If in WebView below version 5.0, if you first remove WebView in parent, then WebView will not be able to destroy, which will cause memory leakage. You can try this statement for yourself.
One of the problems encountered now is that when WebView is nested in ScrollView, some models will have the problem of splash screen, which will not occur when WebView is alone. After the hardware acceleration is turned off, the user experience is not good, so a better solution has not been thought of yet, so it is still recommended not to nest controls like WebView in ScrollView.
These are all the contents of the article "how to use android's WebView". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.