Interaction between Android and JavaScript (mutual call of methods)

Source: Internet
Author: User

Interaction between Android and Javascript. Javascript needs to run in a browser or android webview component. Javascript must have a carrier HTML file.

Put it under the assets folder of the android project.

First, I will post the code first:

The content of the Main. xml layout file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Edittext
Android: Id = "@ + ID/TXT"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

<Button
Android: Id = "@ + ID/BTN"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "send to JavaScript"/>

<Button
Android: Id = "@ + ID/btnno"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "send to JavaScript (No parameter)"/>

<Textview
Android: Id = "@ + ID/Show"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

<Webview
Android: Id = "@ + ID/WV"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>

</Linearlayout>

The HTML content in the assets folder is as follows:

<! Doctype HTML public "-// wapforum // dtd xhtml mobile 1.0 //" http://www.wapforum.org/DTD/xhtml-mobile10.dtd ">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script language = "JavaScript" type = "text/JavaScript">
Function get4android (STR ){
Document. getelementbyid ("show"). innerhtml = "this is a message from Android :"
+ STR;
}
Function get4androidnoparameter (){
Alert ("get4androidnoparameter ");
}
Function send2android (){
VaR STR = Document. getelementbyid ("mess"). value;
Window. myjs. runonandroidjavascript (STR); // call the android Function
}
Function send2androidnoparameter (){
Window. myjs. runonandroidjavascript (); // call the parameter-free function of Android.
}
</SCRIPT>
</Head>

<Body>
<Input type = "text" id = "mess"/>
<Input type = "button" value = "send to Android (having parameters)" onclick = "send2android ()"/>
<Input type = "button" value = "send to Android (no parameters)" onclick = "send2androidnoparameter ()"/>
<Div id = "show"> </div>
</Body>
</Html>

The activity code is as follows:

Package com. BroadVision. jstest;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. WebKit. webchromeclient;
Import Android. WebKit. websettings;
Import Android. WebKit. webview;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. textview;
Import Android. widget. Toast;

Public class jstestactivity extends activity {
Private edittext txt;
Private webview WV;
Private button BTN;

Private button btnno;
Private handler H = new handler ();

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
TXT = (edittext) findviewbyid(r.id.txt );
WV = (webview) findviewbyid (R. Id. WV );
BTN = (button) findviewbyid (R. Id. BTN );
Btnno = (button) findviewbyid (R. Id. btnno );
WV. requestfocus (view. focus_down | view. focus_up );
Websettings = WV. getsettings ();

Websettings. setjavascriptenabled (true );
Websettings. setlighttouchenabled (true );
Websettings. setsaveformdata (false );
Websettings. setsavepassword (false );
Websettings. setsuppzoom zoom (false );

// Do not use addjavascriptinterface () unless all of the HTML in this
// Webview was written by you in general, Java and JavaScript cannot call each other unless you know that JavaScript code is safe.
// The Java object that is bound runs in another thread and not in
// Thread that it was constructed in. myjs is a custom interface for JavaScript access.
WV. addjavascriptinterface (New runjavascript (), "myjs ");
// The webchromeclient can invoke "alert ()" js method
WV. setwebchromeclient (New webchromeclient ());

String url = "file: // android_asset/android.html ";
WV. loadurl (URL );

// Webview: the format of the HTML file path under the assets folder to be called is as follows: file: // android_asset/html file name

BTN. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Call the JavaScript function get4android (STR)
Call a javascript constructor with Parameters
WV. loadurl ("javascript: get4android ('" + TXT. gettext (). tostring () + "')");
WV. requestfocus (view. focus_down | view. focus_up );
}
});
Btnno. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
System. Out. println ("No parameter ");
// Call the JavaScript function get4androidnoparameter () Java to call functions without Parameters
WV. loadurl ("javascript: get4androidnoparameter ()");
WV. requestfocus (view. focus_down | view. focus_up );
}
});
}

// The Java object that is bound runs in another thread and not in the thread
// That it was constructed in. One sentence in the document!
Class runjavascript {// This Java object is bound to another thread,
Public void runonandroidjavascript (final string Str ){
H. Post (New runnable (){
@ Override
Public void run () {// special note here
Textview show = (textview) findviewbyid (R. Id. Show );
Show. settext ("this is a message from javascript:" + Str );
WV. requestfocus (view. focus_down | view. focus_up );
}

});
}
Public void runonandroidjavascript (){
H. Post (New runnable (){
@ Override
Public void run () {// special note here
Toast. maketext (jstestactivity. This,
"This is a message from JavaScript", Toast. length_long). Show ();
WV. requestfocus (view. focus_down | view. focus_up );
}

});
}
}
}

Note:

1. Java can be overloaded based on parameters with the same method name. It is best not to use JavaScript like this.
2. webview. setwebchromeclient (New webchromeclient (); after the Javascript alert method is set, it can be called in webview.
3. websettings. setlighttouchenabled (true); and webview. requestfocus (view. focus_down | view. focus_up );
Communication to solve the problem that the input boxes in edittext and webview cannot obtain the focus respectively.

Project

I hope it will help you. If you need this project, I can send it to you.

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.