Android WebView Development Tutorials

Source: Internet
Author: User

Declaration in the first: must be in the androidmainfest.xml inside the permission, otherwise written in Java All webview Browse the page code is not working properly

<uses-permission android:name= "Android.permission.INTERNET"/>  

Use of 1.WebView

(a). Create an instance of WebView to join the activity

WebView WebView = new WebView (this);  Setcontentview (WebView);  

or configure WebView in XML

<Webview      android:layout_width= "match_parent"      android:layout_height= "Match_parent" >  </Webview>   

(b). Visit the Web page

Webview.loadurl ("https://www.baidu.com/");  

2.WebView API Usage

(a). Request to load a webpage section
public void LoadData (string data, String MimeType, String encoding)  

Load the specified data

Parameter description:

Data strings in string form can be encoded by base64

Minetype the MIME type of data, e.g. ' text/html '

Encoding Data encoding format

Attention:

1.Javascript has a homologous limitation, and the same-origin policy restricts the way in which a source is loaded or the script interacts with data from other sources. To avoid this limitation, you can use the Loaddatawithbaseurl () method.

The 2.encoding parameter is set to whether the data parameter is base64 or URL encoded, and if data is Base64 encoded then encoding must fill in "base64".

————————————————————————————————————————————————————

public void Loaddatawithbaseurl (string baseUrl, String data, String MimeType, String encoding, String historyurl)  

Using BaseURL to load the Web page content of the base URL, BaseURL addresses the same source problem using JavaScript for the relevant URL.

————————————————————————————————————————————————————

public void Loadurl (String URL)  

Load Web page content that develops URLs

————————————————————————————————————————————————————

public void Loadurl (String url, map<string, string> additionalhttpheaders)  

Loads the development URL and carries the HTTP header data.

————————————————————————————————————————————————————
public void Reload ()  

Reload page Note: All resources on the page are Reloaded

————————————————————————————————————————————————————
public void stoploading ()  

Stop loading page

(b). Forward and backward pages

public void GoBack ()  //page back public void GoForward ()  //page forward public void Gobackorforward (int steps)//with current index Forward or back to the steps specified in the history, or  backward if steps is negative, positive for forward public boolean cangoforward ()  //To determine if the page can advance public boolean CanGoBack ()  //Determine if the page can be back

(c). JavaScript operations

public void Addjavascriptinterface (Object object, String name)  

When a Web page needs to interact with the app, you can inject Java objects into the JAVASCRITP call. Java objects provide the appropriate method for JS to use.

Note: Using this API under Android 4.2 will involve JavaScript security issues, and JavaScript can attack by reflecting the related classes of this Java object.

Links: Android WebView summarizes--java and JavaScript interactions

Link:JS vulnerability exists with WebView component Addjavascriptinterface method under Android 4.2 version

————————————————————————————————————————————————————
public void Evaluatejavascript (String script, valuecallback<string> resultcallback)  

This method is introduced in the Android 4.4 system and is therefore only available in the Android4.4 system, providing asynchronous execution of JavaScript code in the context of the current page display

Note: This method must be called on the UI thread, and the callback for this function will also be executed on the UI thread. So how do you execute Javascrit code in Android4.4? The Loadurl method that can be provided by WebView: the specific format is as follows:

Webview.loadurl ("Javascript:alert (injectedobject.tostring ())");  

Where javascript: is the identifier for executing JavaScript code, followed by a JavaScript statement.

————————————————————————————————————————————————————
public void Removejavascriptinterface (String name)  

Java object injected to WebView when Addjavascripinterface is deleted. This method WebView on different Android systems and there will be a failure condition.

(d). Web search function

public void Findallasync (String find)  

Asynchronously executes a search for characters contained within a Web page and sets highlighting, and the lookup results are recalled.

————————————————————————————————————————————————————

public void FindNext (Boolean forward)  

Finds the next matching character

(e). WebView Event Callback Listener

public void Setwebchromeclient (Webchromeclient client)  

Mainly notifies the client app to load events such as Title,favicon,progress,javascript dialog of the current webpage, notifying the client to handle these corresponding events.

————————————————————————————————————————————————————
public void Setwebviewclient (Webviewclient client)  

The main notification client app loads the current page when the various timing states, onpagestart,onpagefinish,onreceiveerror and other events.

Android WebView Development Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.