Android webview and webview

Source: Internet
Author: User

Android webview and webview

1. WebvView is a built-in webkit high-performance internal browser of android.

2. (1) permission: <uses-permission android: name = "android. permission. INTERNET"/>

(2) If Html has JavaScript, you need to set webView. getSettings (). setJavaScriptEnabled (true );

(3) If a connection exists on the page, you must set Webview. setWebViewClient (new WebViewClient () to continue opening the connection by clicking the link in webView. Otherwise, the Webview will be opened by calling the browser of the system.

NewWebViewClient (){

@ Override

Public BooleanShouldOverrideUrlLoading (WebView view, String url ){

}

The shouldOverrideUrlLoading method of the WebViewClient class. This method is called when a connection is opened. The url is the webpage address to be opened. you can do some operations before opening a specific webpage, or do not open the webpage

3. How to open the page

(1) Open the Internet page

WebView. loadUrl ("http://www.google.com"); // get form

WebView. postUrl (url, EncodingUtils. getBytes (postData, "UTF-8"); // Post form. url, postData parameter: Value

(2) open a local file

MyWebView. loadUrl ("file: // android_asset/XX.html")

The file is stored in the assets Directory and starts with file: // android_asset/. xx.html is the file name.

(3) directly load html strings

MyWebView. loadData (htmlString, "text/html", "UTF-8 ");

 

4. backward and forward

When you press the return key, if no processing is performed, the entire Browser is exited. If you want to return to the previous page, use webView. goBack (); forward to use webView. goForward (); if these two actions are executed at the top of the history, no action will be executed. Therefore, before the execution, you must first make a judgment: webView. canGoBack () and webView. canGoForward ().

@ Override

Public BooleanOnKeyDown (IntKeyCode, KeyEvent event ){

If(KeyCode = KeyEvent.KEYCODE_BACK& WebView. canGoBack ()){

WebView. goBack ();

Return True;

}

Return Super. OnKeyDown (keyCode, event );

}

 

 

5. WebView interaction with javascript

(1) javascript accessing java code

Java code: webView. addJavascriptInterface (new Object (){

Public void onBack (){

}

}, "Test ");

Javascript: <a onClick = "window. test. onBack ()">.

 

AddJavascriptInterface (Object, String) Method

Test is the interface name exposed to JS, corresponding to the Object instance. javascript calls the onBack () method of the Object through window. test. onBack.

When an Object is called, another thread is started.

 

(2) java accesses javascript

MWebView. loadUrl ("javascript: test ('aa')"); where test is the javascript method.

You can use the data alert in the test function, and then rewrite the onJsAlert function of WebChromeClient in webview. The specific code is as follows:

[Java] view plaincopy

Wv. setWebChromeClient (new MyWebChromeClient ());

 

Final class MyWebChromeClient extends WebChromeClient {

 

@ Override

 

Public booleanonJsAlert (WebView view, String url, String message, final JsResult result ){

 

// Message is the alert string in the test function, so that you can process the data in the android client.

Result. confirm ();

 

}

 

Return true;

}

 

6. Customize a WebView with a progress bar

Public class ProgressWebview extends WebView {

Private ProgressBar progressBar;

 

Public ProgressWebview (Context context, AttributeSet attrs ){

Super (context, attrs );

ProgressBar = new ProgressBar (context, null, android. R. attr. progressBarStyleHorizontal );

ProgressBar. setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, 4,0, 0 ));

AddView (progressBar );

SetWebChromeClient (new WebChrmoeClient ());

}

 

/*

* Display progress bar

*/

Public class WebChrmoeClient extends WebChromeClient {

@ Override

Public void onProgressChanged (WebView view, int newProgress ){

If (newProgress = 100 ){

ProgressBar. setVisibility (View. GONE );

} Else {

If (progressBar. getVisibility () = GONE ){

ProgressBar. setVisibility (View. VISIBLE );

}

ProgressBar. setProgress (newProgress );

}

Super. onProgressChanged (view, newProgress );

}

@ Override

Public boolean onJsAlert (WebView view, String url, String message,

JsResult result ){

// TODO Auto-generated method stub

Return super. onJsAlert (view, url, message, result );

}

}

@ Override

Protected void onScrollChanged (int l, int t, int oldl, int oldt ){

// TODO Auto-generated method stub

LayoutParams params = (LayoutParams) progressBar. getLayoutParams ();

Params. x = l;

Params. y = t;

ProgressBar. setLayoutParams (params );

Super. onScrollChanged (l, t, oldl, oldt );

}

}

Related Article

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.