JavaScript and Java interaction on Android WebView

In fact, the speed of WebView loading resources is not slow, but if there are many resources, of course, it is very slow. Images, CSS, JS, HTML and other resources need about 10-200ms each, usually 30ms. However, WebView cannot render until all resources are loaded, so the loading speed is very important! From Google, we know that the order in which WebView loads the page is: first load HTML, and then parse CSS, JS files and image resources on the page to load. If there is in the WebKit cache, it will not be loaded. After loading these resources, you can render CSS and execute JS. The rendering of CSS generally does not take a long time, and it takes tens of milliseconds. The key is the execution of JS. If jQuery is used, it takes 5-6 seconds to execute. During this time, if the background is not set in WebView, the part of the web page is white and ugly. This is a terrible user experience. So # o # bertek # suggests that if you use a web page layout program, you'd better not use those huge JS frameworks. It is best to write business scripts using native JS to improve loading speed and user experience.

In mixed development, Android native SDK is sometimes used, such as calling camera, viewing photo album, recording, etc., which requires JavaScript in web page to call Android SDK interface. Because Android WebView is based on WebKit kernel and integrates the interface functions of JS and Java intermodulation, it can be developed and used conveniently.

Interface layout XML:

Java code:

Among them: there are several points that must be noted. There are several changes in the early online description of WebView. 1) WebView is used to realize java and JS intermodulation in Android versions above 4.2. The java interface needs to declare @ javascriptinterface; 2) WebView needs to call setwebchromeclient() to adapt to JS pop-up window and other implementations; 3) the JavaScript interface is invoked in the binding interface in addJavascriptInterface, and it needs to be invoked by the sub thread (false report: Caused by: java.lang.Throwable: A WebView method was called on called)

HTML code:

In the above HTML file, javainterface is the name of the interface entry injected in the addjavascriptinterface () method in WebView. Through this name, you can directly call the interface in Java. (the HTML page needs to be kept in the project assets directory, which is controlled by webview.loadurl(“ file:///android_asset/index.html ") to load);

design sketch:

The dialog box pops up: the web page with the web address "file: / /", if it is a web page on the server, the source IP address will be displayed, etc., which is obviously not what we want. Next, we can rewrite the webchromeclient to modify the optimization of WebView such as dialog box and confirmation box.

The above content is all about the interaction between JavaScript and Java on Android WebView. I hope it will be helpful to you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>