Basic configuration and pit filling records of WebView in Android

preface

In the process of application development, WebView is often used to show some interfaces, so that it can be updated in real time without release version control, and problems can be repaired quickly.

However, in Android development, due to the serious differentiation of Android versions, each version has some changes for WebView, so various pits will be encountered in the development process. The following article will introduce you to the basic configuration and pit filling records of WebView in Android. I won't say much. Let's take a look at the detailed introduction together.

Basic configuration

Pit filling record

1. Common methods of webviewclient class

shouldOverrideUrlLoading()

All loads on the web page go through this method. We can do many operations with this function.

onPageStarted()

To start loading page calls, we can set a loading page to tell the user that the program is waiting for a network response.

onPageFinished()

Called at the end of page loading. We can close the loading bar and switch program actions.

onLoadResource()

It will be called when loading page resources, and each resource (such as pictures) will be called once.

onReceivedError()

Called when there is an error loading the page.

2. Common methods of webchromeclient class

onProgressChanged()

Get the loading progress of the web page and display it.

onReceivedTitle()

Gets the title in the web page.

onJsAlert()

Supports JavaScript warning boxes.

onJsConfirm()

Support JavaScript confirmation box.

onJsPrompt()

Supports JavaScript input boxes.

3. Mixed mode of HTTPS and http

Starting from Android 5.0, WebView does not support loading HTTPS and HTTP resources at the same time by default.

Solution: set the loading mode to mixed before the WebView loads the page_ CONTENT_ ALWAYS_ ALLOW

4. Security issues

addJavascriptInterface

Before Android 4.2, the vulnerability should be repaired by intercepting prompt(); After Android 4.2, you only need to annotate the called function with @ javascriptinterface.

search@R_ 149_ 2419@JavaBridge_ 、 accessibility、accessibilityTraversal

Password plaintext storage vulnerability

5. Replace WebView's load error page

6. WebView has a memory leak.

It's hard for me to give you a clear description of this problem. You can find many results by searching WebView lead memory in Google, and even the issue submitted to Google. Ha ha, I can't give a clear answer. When and what version of those mobile phones will leak memory,

However, according to my own monkey results, sometimes the memory leakage of WebView is very serious, especially when you load a large page. I checked and used a lot of solutions, but they were not ideal. Finally, I looked at the practices of wechat and QQ and tried them. They are the best at present,

When you want to use WebView, remember that it's best to open another process to use WebView, and when the process ends, please manually call system. Exit (0).

This is the best solution for WebView memory leakage. Using this method, all problems such as the inability to release resources caused by WebView can be solved.

7. Getsettings(). Setbuiltinzoomcontrols (true) throws a crash.

After this method is called, if you touch the screen and the prompt box pops up, you will report an error if the activity ends. This happens on many mobile phones above 3.0 and below 4.4

Therefore, in order to avoid it, we usually manually set webiew to setvisibility (view. Gone) in the ondestroy method of activity

8. Is the onpagefinished function useful?

Most developers refer to it http://stackoverflow.com/questions/3149216/how-to-listen-for-a-webview-finishing-loading-a-url-in-android This is the high ticket answer above.

But in fact, according to my own observation, this function is useless. Sometimes it ends early, and sometimes it can't end late. If you believe in this function, you'd better believe in God, or even onprogresschanged

Better than onpagefinished. If your product manager insists that you must realize this function, I suggest you end it early, otherwise the users stuck there can't move for a long time. This experience is not good.

If you are free, you can follow the source code. Onpagefinished is called at different times in different kernels. To tell you the truth, I'm also drunk... There is a perfect solution to this problem. Please let me know...

9. The background cannot release JS, resulting in power consumption.

Few people may know this. I learned it only after being complained. In some mobile phones, if there are some JS in the HTML loaded by WebView, such as animation, if WebView is hung in the background at the moment

These resources will not be released or perceived by users... As a result, the power consumption of the occupied CPU is very fast, so remember, if this happens, please set JavaScript enabled() in onstop and onresume respectively;

Set to false and true for.

10. If you really don't want to solve the problem of WebView memory leakage by opening additional processes, the following methods can largely avoid this situation

Just call this method in the destroy method of WebView.

11. In addition, many people don't know that WebView actually has its own complete cookie mechanism. Making good use of this can greatly increase the access speed to the client.

In fact, cookies are stored in this table.

Many people want an effect: the web page update cookie can take effect without refreshing the page after setting the cookie. The methods to be implemented below 2.3 and above 2.3 are different, so compatibility should be done once

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>