Android app interface using html

Source: Internet
Author: User

 

In the past, hackers used web pages for the speed edition and the network shield client. So I have a special liking for this mode. The webview component in android carries the webkit kernel, V8 script engine, and HTML5. If you use this to create some online application interfaces, it should be a good choice. Although the android writing interface is already quite simple (compared to the painful memory of writing a GUI using Java Swing five years ago), online web pages naturally offer online benefits. In addition, HTML5's offline storage function is also good. I spoke a bunch of nonsense. Sorry.

 

First, create a project and use the webview component.

 

Demo code core:

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

 

MWebView = (WebView) this. findViewById (R. id. webview );

MHandler = new Handler ();

 

WebSettings webSettings = mWebView. getSettings ();

WebSettings. setJavaScriptEnabled (true );

MWebView. addJavascriptInterface (new Object (){

Public void clickOnAndroid (){

MHandler. post (new Runnable (){

Public void run (){

MWebView. loadUrl ("javascript: wave ()"); // call the Script Function

}

});

}

}, "Demo"); // provides an interface for the script function, window. demo. clickOnAndroid ()

MWebView. loadUrl ("file: // android_asset/html/notice.html ");

Note the following points:

(1) If the application needs to access the Internet, add the following to AndroidManifest. xml:

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

(2) In the code, android_asset is equivalent to the resources under the assets directory in the project path.

(3) setJavaScriptEnabled is used to set whether javascript scripts are allowed and must be enabled.

(4) In main. xml under layout, <WebView android: id = "@ + id/webview" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/>

 

 

This demo is successfully completed. There are two main points in this demo: 1. Use the webpage as the interface, 2. Mutual calls between JavaScript and Java are implemented.

 

 

Author-274 °C

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.