Web pages and Nativecode interactions in Android programs

Source: Internet
Author: User

The first thing you should do in androidmanifest.xml is add the following to make network access

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

Then insert the WebView main.xml on the layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >    <WebViewAndroid:id= "@+id/webview1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "1" />        <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"Android:layout_weight= "1">     <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:textappearance= "? Android:attr/textappearancelarge" />   </LinearLayout></LinearLayout>

Local code creates an instance of Javascriptinterface

Final New Javascriptinterface (this);

Then add the interface

Wv.addjavascriptinterface (Myjavascriptinterface, "androidfunction");

This WV is the WebView instance androidfunction is the JavaScript function used on the HTML page.

When we call androidfunction in HTML, we can switch to native code.

The entire code for Java is as follows

  

 PackageMy.demo;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.webkit.WebView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classJavascriptinterfacedemoactivityextendsActivity {PrivateWebView Wv; PrivateTextView Mytextview; FinalHandler MyHandler =NewHandler (); /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Wv=(WebView) Findviewbyid (R.ID.WEBVIEW1); Mytextview=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Finaljavascriptinterface Myjavascriptinterface=NewJavascriptinterface ( This); Wv.getsettings (). setlighttouchenabled (true); Wv.getsettings (). setjavascriptenabled (true); Wv.addjavascriptinterface (Myjavascriptinterface,"Androidfunction"); Wv.loadurl ("File:///android_asset/www/index.html"); }         Public classjavascriptinterface {Context mcontext; Javascriptinterface (Context c) {Mcontext=C; }                 Public voidshowtoast (String webmessage) {FinalString Msgetoast =Webmessage; Myhandler.post (NewRunnable () {@Override Public voidrun () {//This gets executed on the UI thread so it can safely modify viewsMytextview.settext (msgetoast);           }             });        Toast.maketext (Mcontext, Webmessage, Toast.length_short). Show (); }    }}

The HTML page is as follows

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet=utf-8 "/>        <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">        <Metaname= "Apple-mobile-web-app-capable"content= "Yes">        <Metaname= "Viewport"content= "target-densitydpi=device-dpi" />        <Scripttype= "Text/javascript">           functioninit () {varTestVal=document.getElementById ('Mytextid'). Value;           Androidfunction.showtoast (TestVal); }        </Script>    </Head>    <Body>                <Divstyle= "float:left;width:50%;">           <inputtype= "text"style= "width:180px;"name= "MyText"ID= "Mytextid" />                   </Div>        <Divstyle= "CLEAR:BOTH;HEIGHT:3PX;"> </Div>        <Div>          <inputvalue= "Submit"type= "button"name= "Submit"ID= "Btnsubmit"onclick= "Javascript:return init ();" />         </Div>      </Body></HTML>

Relatively simple and convenient

  

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.