Introduction to WebView loading optimization methods
This article brings you an introduction to the method of WebView loading optimization. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
WebView loading optimization
When the frequency of use of WebView becomes frequent, the optimization of all aspects of it becomes increasingly important. What you can know is that every time we load an H5 page, there will be a lot of requests. In addition to the request of the HTML main URL itself, the JS, CSS, font files, and images referenced externally by the HTML are all independent HTTP requests. Although the requests are concurrent, when the overall number of web pages reaches a certain level, plus The browser parsing and rendering time, and the overall loading time of the Web become very long. The more files requested at the same time, the more traffic will be consumed. Then the optimization of loading becomes very important. I have no other experience in this area. There are probably three aspects:
One is the issue of resource localization
First of all, it is clear that with the current Due to network conditions, the speed of obtaining resources from the server through the network is far slower than reading them locally. Talking about various optimization strategies actually ignores the fact that "requiring loading" is the biggest stumbling block to speed improvement. So our first idea is to localize some heavier resources such as js, css, pictures and even HTML itself. Every time these resources are loaded, they are read and loaded from the local area. They can be simply remembered as "save". ·Get·Update".
1. "Save" - package the above heavyweight resources into apk files, and fetch them locally every time the corresponding files are loaded. You can also not package it, dynamically download and store it during the first load and at subsequent intervals, and store all resource files in the Android asset directory;
2. "Get" - Rewrite the WebResourceResponse of WebViewClient The shouldInterceptRequest(WebView view, WebResourceRequest request) method intercepts the corresponding request through a certain identification method (such as a regular expression), reads the corresponding resource from the local and returns it;
3. "Update" - Establish a Cache Control mechanism , control the updates of local resources regularly or in the form of API notifications to ensure that local resources are up to date and available.
The second one is the problem of caching
If you do not adopt or do not fully adopt the first resource localization idea, then your WebView cache must be turned on (although this idea is different from the first idea) There are overlaps).
WebSettings settings = webView.getSettings(); settings.setAppCacheEnabled(true); settings.setDatabaseEnabled(true); settings.setDomStorageEnabled(true);//开启DOM缓存 settings.setCacheMode(WebSettings.LOAD_DEFAULT);
When the network is normal, the default caching strategy is adopted, and the cache is loaded when the cache is available and has not expired. Otherwise, resources are obtained through the network to reduce the number of network requests for the page.
It’s worth mentioning here that when we often use WebView to display pages in apps, we don’t want the user to feel that he is visiting a web page. Because if there are too many web pages in our app, and we give users the feeling that they are visiting web pages, our app will lose its meaning. (I mean why don't users use the browser directly?)
So at this time, the issue of offline caching deserves our attention. We need to allow users to still operate our app when there is no Internet, instead of facing a page that is the same as the network error in the browser, even if the operations he can perform are very limited.
My idea here is that under the premise of turning on caching, WebView detects network changes when loading the page. If the user's network is suddenly disconnected when loading the page, we should change the caching strategy of WebView.
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if(networkInfo.isAvailable()) { settings.setCacheMode(WebSettings.LOAD_DEFAULT);//网络正常时使用默认缓存策略 } else { settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);//网络不可用时只使用缓存 }
Since there is a cache, there must be cache control. Similar to this, we must also establish a cache control mechanism to clear or update the cache regularly or by accepting server notifications.
The third one is to delay loading and executing js
In WebView, the callback of onPageFinished() means the completion of page loading. However, this method will not be triggered until the JavScript script is executed. If the page we want to load uses JQuery, it will not be rendered until the DOM object has been processed and $(document).ready(function() {}) has been executed. and display the page. This is unacceptable, so we need to lazy load Js. Of course, this part is the work of the web front-end.
This article has ended here. For more exciting content, you can pay attention to the Java Tutorial Video column of the PHP Chinese website!
The above is the detailed content of Introduction to WebView loading optimization methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
