Android webview settings

Source: Internet
Author: User

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. "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 on the web, JavaScript Execution is required:

JavaCode:

    1. Webview webv = (webview) findviewbyid (R. Id. webv );
    2. // Obtain webview from XML
    3. Webv. getsettings (). setjavascriptenabled (true );
    4. // Allows JS execution

Copy code

3. If we do not want to create a webview process when using webview for applicationProgramPerform the following settings

Java code:

    1. Webv. setwebviewclient (New webviewclient (){
    2. Public Boolean shouldoverrideurlloading (webview view, string URL ){
    3. View. loadurl (URL );
    4. // Load the URL on the original process again when you click the hyperlink
    5. Return true;
    6. }
    7. });

Copy code

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

Java code:

    1. File: // android_asset/teste.html: The teste.html file under assets.
    2. File: // index.html file under sdcard/index.html export sdcard

Copy code

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

Java code:

    1. Package EOE. Demo;
    2. Import Android. App. activity;
    3. Import Android. OS. Bundle;
    4. Import Android. WebKit. webview;
    5. Public class sdfsdfsd extends activity {
    6. /** Called when the activity is first created .*/
    7. Webview webv;
    8. @ Override
    9. Public void oncreate (bundle savedinstancestate ){
    10. Super. oncreate (savedinstancestate );
    11. Setcontentview (R. layout. Main );
    12. Webv = (webview) findviewbyid (R. Id. webv );
    13. Webv. getsettings (). setjavascriptenabled (true );
    14. Webv. addjavascriptinterface (this, "something ");
    15. Webv. loadurl (File: // android_asset/index.html );
    16. }
    17. Public void setsmething (string some ){
    18. System. Out. println ("----------" + some + "---------------");
    19. }
    20. }

Copy code

HTML code:

Java code:

    1. <HTML>
    2. <Head>
    3. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
    4. <Title> </title>
    5. <Script language = "JavaScript" type = "html/text">
    6. Function dosomething (){
    7. Document. getelementbyid ("helloweb"). innerhtml = "hellowebview ";
    8. }
    9. </SCRIPT>
    10. </Head>
    11. <Body onload = "javascript: window. Something. setsmething ('hellowebview')">
    12. <Div id = "helloweb">
    13. </Div>
    14. </Body>
    15. </Html>

Copy code

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.