Android webview settings

Source: Internet
Author: User
Reposted from underalight and finally edited underalight

Webview is equivalent to a browser in Android. developed based on WebKit, webview allows you to browse webpage files, and supports CSS JavaScript and HTML

To use webview, you must first configure the following:

1. androidmanifest. xml must register "android. Permission. Internet" for permission. (If you only use local HTML, you do not need to register the permission.) otherwise, the web page not available error may occur.
.

2. if Javascript is used in the Web, JavaScript is required to execute: webview webv = (webview) findviewbyid (R. Id. webv); // obtain webview from XML

Webv. getsettings (). setjavascriptenabled (true); // allows JS execution

3. If we do not want to create a webview process when using webview for application, and let the program jump off, perform the following settings:

Webv. setwebviewclient (New webviewclient (){

Public Boolean shouldoverrideurlloading (webview view, string URL ){

View. loadurl (URL); // load the URL on the original process again when you click the hyperlink.
Return true;

}

});

4. During webview development, the HTML files of the local machine are often loaded as follows:

File: // android_asset/teste.html: The teste.html file under assets.

File: // index.html file under sdcard/index.html export sdcard

5. Call the Java method in Javascript

5.1 bind a current Java object to a JavaScript Object first. Use the following method:

Webv. addjavascriptinterface (this, "something"); // This is the current object and bound to the something of JS. The scope of the main something is global. Once initialized, it can run anywhere

5.2 define the called Java method


Example

Java code:

Package cc. CDC;

 

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. WebKit. webview;

 

Public class sdfsdfsd extends activity {

/** Called when the activity is first created .*/

Webview webv;

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Webv = (webview) findviewbyid (R. Id. webv );

Webv. getsettings (). setjavascriptenabled (true );

Webv. addjavascriptinterface (this, "something ");

Webv. loadurl ("file: // android_asset/index.html ");

}

Public void setsmething (string some ){

System. Out. println ("----------" + some + "---------------");

}

}

HTML code:

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> </title>

<Script language = "JavaScript" type = "html/text">

Function dosomething (){

Document. getelementbyid ("helloweb"). innerhtml = "hellowebview ";

}

</SCRIPT>

</Head>

<Body onload = "javascript: window. Something. setsmething ('hellowebview')">

<Div id = "helloweb">

 

</Div>

</Body>

</Html>

The result of this example is to call the Java setsmething method after loading the HTML page to print the string result.

This also needs to be triggered when JavaScript code is called in Java. Now, the onload code in the preceding HTML is changed to onload = "javascript: window. Something. getjsdata ()"

Then add the following code in Java:

Public void getjsdata (){

Webv. loadurl ("javascript: dosomething ()");

}

Execution result

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.