Android App hosted web page (WebView)

WebView will play an important role in how Android App opens Web pages and how to make a simple browser. WebView is a control that displays web pages based on WebKit engine.

WebView is a control based on WebKit engine, which can parse DOM elements and display HTML pages. Its principle is the same as that of the browser, so it can be regarded as a browser. (Chrome browser is also developed based on WebKit engine, and Mozilla browser is developed based on gecko engine)

In short, WebView can host web pages in android app

Mobile phone browser:

The built-in browser of domestic mobile phones is not chrome, mainly because of copyright. The built-in browsers are customized by mobile phone manufacturers based on several domestic mainstream browsers and then published in their own mobile phone system version. However, several domestic browser manufacturers such as QQ browser and UC browser are based on WebKit engine, and the built-in browser of iPhone is safari, The kernel of safari browser is WebKit

Benefits of using WebView:

Load the web page of the server

A web page that loads resource files (assets directory)

The assets directory is a special directory for Android. It is used to place the fixed files required by the app. When the file is packaged in APK, it will not be encoded into binary files.

Attach web page code when web page is loaded

Baseurl represents the underlying web page

Data represents the content to load

MimeType indicates the type of web page to load

Encoding indicates the encoding format

Historyurl indicates available history, which can be null

Can cangoback() go back

Can cangoforward() move forward

Cangobackorforforward (int step) whether or not you can move forward or backward. A positive number is forward and a negative number is backward

Go back

Goback() forward

Gobackorforforward (int step) the number of steps forward or backward. A positive number is forward and a negative number is backward

Reload() reloads or refreshes the interface

Stoploading() stops loading

realization:

Various notification and request events (webviewclient)

Onpagestarted() page loading

Onpagefinished(): called when the page is loaded.

Shouldoverridekeyevent(): override this method to handle key events in the browser.

Shouldinterceptrequest(): this method will be called before the page requests resources every time (non UI thread call).

Onloadresource(): called when the page loads resources. It is called every time a resource (such as a picture) is loaded.

Onreceivederror(): callback when the server loading the page has an error (such as 404).

Onreceivedsslerror(): overriding this method allows WebView to process HTTPS requests.

Doupdatevisitedhistory(): update history.

Onformresubmission(): the application re requests web page data.

Onreceivedhttpauthrequest(): get the return information authorization request.

Onscalechanged(): called when a scaling change occurs in the WebView.

Onunhandledkeyevent(): called when the key event is not loaded.

Assist WebView to handle supplementary problems (webchromeclient)

Onprogresschanged(): get the loading progress of the web page and display it.

Onreceivedtitle(): callback when getting the title of the web page.

Onreceiveicon(): callback when getting the icon of the web page.

Oncreatewindow(): callback when opening a new window.

Onclosewindow(): callback when closing the window.

Onjsalert(): this method is triggered when a prompt box pops up on the web page.

Unified steps:

Step 1:

Add network access permission in androidmanifest.xml. If you need external network access

Step 2:

Add WebView control under XML

Here, WebView will be used to open the blog home page of Xiaodong in the blog Garden

Step 3:

Write logic program

The assets directory is a special directory for Android. It is used to place the fixed files required by the app. When the file is packaged in APK, it will not be encoded into binary files.

Step 3:

Write HTML code and save it to the file android.html

Step 4

Create assets directory

Right click app - New - folder - assets folder - finish

Step 5

Copy the HTML file to the assets directory

Step 6

Directly write part of the HTML code, which can be directly displayed on WebView

Step 3:

Write logic program

Disadvantages: the project does not realize the audio and video playback of web pages

Implementation functions:

L title display

L web page loading progress bar display

L adaptation of four common functions: back, forward, refresh and stop loading

L connection jump, open the web page locally in the app

View source code

Logic source code

reference resources:

https://blog.csdn.net/Jolting/article/details/81223904? utm_ source=blogxgwz9

https://blog.csdn.net/weixin_ 40438421/article/details/85700109

https://www.jianshu.com/p/3e0136c9e748

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