Usage resolution of WebView components developed by Android

  • 2020-05-07 20:25:49
  • OfStack

A high-performance webkit kernel browser is built into the Android phone, and a component called WebView is packaged into SDK.
The WebView class is the view class of the Java layer of the WebKit module (all Android applications that require Web browsing will create the network resources that this view object displays and handles requests for. Currently, the WebKit module supports HTTP, HTTPS, FTP, and javascript requests.
As the UI interface of the application program, WebView provides users with 1 series of web browsing and user interaction interfaces through which the client program can access the WebKit core code. )

What is webkit
WebKit is the software framework included in Mac OS X v 10.3 and above (also available through software update for v 10.2.7 and above).
WebKit is also the basis of Safari web browser for Mac OS X. WebKit is an open source project that is largely modified from KDE's KHTML and includes some components from apple.
Traditionally, WebKit consists of one web engine WebCore and one scripting engine JavaScriptCore, which correspond to KHTML and KJS of KDE, respectively.
However, as the JavaScript engine became more and more independent, WebKit and WebCore are now largely intermixed (e.g. Google Chrome and Maxthon 3 use V8 engines, but still claim to be the WebKit kernel).
Here we have a preliminary experience 1 under android is using webview to browse the web,

SDK Dev Guide has 1 simple example in WebView. A few points should be noted during the development process: 1. AndroidManifest.xml must use the license "android.permission.INTERNET ", otherwise the Web page not available error will occur.
2. If Javascript is present on the page being visited, webview must be set to support Javascript. webview. getSettings (.) setJavaScriptEnabled (true);
3. If there is a link in the page, and if you wish to click the link to continue responding in the current browser rather than in the system browser, which is newly opened with Android, you must overwrite the WebViewClient object of webview.
mWebView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } });
mWebView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } });
4. If you do not do any processing, browse the website, and click the system "Back" key, the whole Browser will call finish() and end itself.
If you want to browse the web page back rather than out of the browser, you need to process and consume the Back event in the current Activity.
 
public boolean onKeyDown(int keyCode, KeyEvent event){ <BR> if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
mWebView.goBack(); 
return true; 
} 
return super.onKeyDown(keyCode, event); 
} 

Put a nice page on tomcat
1. Load the webpage (with permission)
Define 1 url and enter the text field. Click the button to open the page with webView
2. Write data string, webview.loadData (data,"text/html"," UTF-8 ");
3. Open the website with dialog's setiem, move forward, back, zoom in, zoom out, and clear the history.
4. Define the development interface with HTML. file: / android_asset a html
 
<script> 
function loadurl(){ } 
< /script> 
< select name=""> 
<option value=""/> 
<option value=""/> 
< /select> 
webview.getSettings().setJavaScriptEnabled(true); 
webview.getSettings().setBuiltInZoomControls(true); 
webview.loadUrl(file:///...); 

5.js dialog box (with chom..)
 
function openAlert(){ 
window.alert(""); 
} 
function openConfirm(){ 
if(window.confirm(" Do you want to delete this information? ")){ 
window.location="myjs.html";//-------------------!!! 
} 
} 
<input type="submit" value=" warning " onClick="openAlert()"> 
< input type="submit" value=" confirm " onClick="openConfirm()"> 

Methods in java tone js:
 
// in java In the call javascript The method has to go through loadUrl() The method to be called is loadUrl String argument to the method  
settings.setJavaScriptEnabled(true);// Set in the webview Available in the js 
webview.loadUrl("javascript:myprompt1()"); 

Methods in javascript medium java (Android 2.2 simulator is emphasized)
 
webview.addJavascriptInterface(new MyInterface(), "myobj");// The first 2 step --- Registered in js The name of the object invoked in myobj 
webview.loadUrl("file:///android_asset/test.html"); 
// The first 1 Step: define in js Method called in  
class MyInterface{ 
public String getname(){ 
return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
} 
} 

-- -- -- -- -- -- -- -- -- -- -- -- javascript: -- -- -- -- --
 
function myinterface(){ 
document.getElementById("myname").innerHTML = window.myobj.getname() ; 
} 

WebView control is powerful, in addition to having 1 like View properties and Settings, but also to url request, page loading, rendering, page interaction for powerful processing.
WebView has several customizable points:
(1) set WebChromeClient subclass, WebChromeClient will be called when some interaction actions affecting the browser ui occur, such as WebView close and hide, page loading progress, js confirmation box and warning box, js before loading, js operation timeout, webView get focus, etc., see WebChromeClient for details
(2) set WebViewClient subclass, WebViewClient will be called when some actions that affect the clamor of content occur, such as the form error submission needs to be resubmitted, the page starts loading and finishes loading, the resource loading, the receiving http authentication needs to be processed, the page keyboard response, the url in the page opens processing, etc., see WebViewClient for details
(3) set the WebSettings class, which contains several configurations. WebSettings used to configure and manage WebView configuration, such as whether to file operations, whether the cache Settings, page supports zoom in and out, allows database api, fonts and text encoding Settings, whether to allow js script is running, whether to allow images automatic loading, whether to allow data and password saved, etc., see WebSettings
(4) set addJavascriptInterface method, bind java object to webView, so as to facilitate the control of java object from js page, realize the interaction between local java code and html page, and even conduct page automation. However, there are security risks in doing so, so if this method is set, please make sure that the code of webView is done by yourself. See article 5 for detailed use of addJavascriptInterface for automation. Use addJavascriptInterface to complete the interaction with js
1. back key controls page back
Activity's default back key processing is to end the current Activity, webView after viewing a lot of web pages, back wants to press back key to return to the last page, this time we need to cover onKeyDown function, tell him how to handle, as follows:
 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
webView.goBack(); 
return true; 
} 
return super.onKeyDown(keyCode, event); 
} 

webView.canGoBack () returns true when webView contains a reviewable browsing history
webView. goBack (); Represents the last visited page returned to webView
WebView(network view) can load a display page, which can be viewed as a browser.
Network content:
1. LoadUrl directly displays web content (separately displays network pictures)
2. LoadData displays Chinese web content (including processing of Spaces)
APK package files:
1. LoadUrl displays Html and image files in APK
2, LoadData (loadDataWithBaseURL) shows APK Html contents of text and images in the mixed res/layout/main xml
----------------------------------------------------------
Add permissions to the manifest file when prompted for web page not available
< uses-permission android:name="android.permission.INTERNET"/ >
The URLUtil.isNetworkUrl(String uri) method is used to determine whether the URL entered by the user is valid. If not, an Toast information box will be displayed to remind the user to enter the correct URL
----------------------------------------------------------
Steps:
1. Declare WebView in the layout file
2. Instantiate WebView in Activity
3. Call the loadUrl() method of WebView to set the page to be displayed by WevView
4. To enable WebView to respond to the hyperlink function, call the setWebViewClient() method and set the WebView view
5, after looking at many pages with WebView point link, in order to enable WebView to support the fallback function, we need to override the onKeyDown() method of Activity class. If we do nothing, click the system fallback key, and the whole browser will call finish() and end itself, instead of fallback to the previous page
6. You need to add permissions to the AndroidManifest.xml file, or an Web page not available error will appear.
< uses-permission android:name="android.permission.INTERNET"/ >
Here are some specific examples:
MainActivity.java
 
package com.android.webview.activity; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class MainActivity extends Activity { 
private WebView webview; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
webview = (WebView) findViewById(R.id.webview); 
// Set up the WebView Property that can be executed Javascript The script  
webview.getSettings().setJavaScriptEnabled(true); 
// Load the page you want to display  
webview.loadUrl("http://www.8way.com/"); 
// Set up the Web view  
webview.setWebViewClient(new HelloWebViewClient ()); 
} 
@Override 
// Set back  
// cover Activity Of the class onKeyDown(int keyCoder,KeyEvent event) methods  
public boolean onKeyDown(int keyCode, KeyEvent event) { 
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
webview.goBack(); //goBack() Said to return to WebView On the 1 page  
return true; 
} 
return false; 
} 
//Web view  
private class HelloWebViewClient extends WebViewClient { 
@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
view.loadUrl(url); 
return true; 
} 
} 
} 

main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<WebView 
android:id="@+id/webview" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 
</LinearLayout> 
 in AndroidManifest.xml Add permissions to the file  
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.android.webview.activity" 
android:versionCode="1" 
android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="10" /> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".MainActivity" 
android:label="@string/app_name"> 
<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 
</application> 
<uses-permission android:name="android.permission.INTERNET"/> 
</manifest> 

Summary of webview learning records:
First create an webview in the manifest.main file,
Then define webview in activity and do the following.
1. Add permission: the license "android.permission.INTERNET" must be used in AndroidManifest.xml.
2. Generate one WebView component in Activity: WebView webView = new WebView(this);
3. Set WebView basic information:
If Javascript is on the page being visited, webview must be set to support Javascript.
webview.getSettings().setJavaScriptEnabled(true);
Touch focus works requestFocus(); this.setScrollBarStyle (SCROLLBARS_OUTSIDE_OVERLAY);
4 if you want to click the link to be handled by yourself, instead of responding to the link in browser, the new Android system.
Add an event listener to WebView (WebViewClient) and override some of these methods:
shouldOverrideUrlLoading: response to hyperlink buttons in web pages. WebViewClient will call this method when a connection is pressed,
And pass the parameter: press url onLoadResource onPageStart onPageFinish onReceiveError onReceivedHttpAuthRequest
5. If there is Javascript in the page visited, webview must be set to support Javascript; otherwise, a blank page will be displayed.
Java code webview. getSettings (.) setJavaScriptEnabled (true);
6. If there is a link in the page, and if you wish to click the link to continue to respond in the current browser rather than the new browser system browser, you must override the WebViewClient object of webview: Java code 1.mWebView.setWebViewClient (new WebViewClient(){2.3.4.5.6};
The above method tells the system that my WebViewClient will handle the Intent, and I will load URL. Intent is bubbling up by clicking on a link,
shouldOverrideUrlLoading method return true means that when I load this Intent, it consumes, it doesn't bubble up anymore.
7. If you do not do any processing, when displaying your Brower UI, click the system "Back" key, the whole Browser will be a whole "Back"}
public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true;
Go to other Activity instead of Back as desired in the Browser history page.

Related articles: