admin管理员组

文章数量:1287265

I need to make some UI part in javascript for android application for that I'm writing some code in javascript and load it in android web view. Now, I want to call java class from javascript.

Is it possible to call java class from javascript?

Thanks in Advance!

I need to make some UI part in javascript for android application for that I'm writing some code in javascript and load it in android web view. Now, I want to call java class from javascript.

Is it possible to call java class from javascript?

Thanks in Advance!

Share Improve this question edited Jul 7, 2011 at 5:38 Buhake Sindi 89.2k30 gold badges175 silver badges232 bronze badges asked Jul 7, 2011 at 5:31 milindmilind 9704 gold badges12 silver badges39 bronze badges 2
  • Why are you splitting the application that way? Why not just write it all,including UI, in Java? – Soren Commented Jul 7, 2011 at 5:34
  • because i need to draw much plex UI in android you can see the following question at stackoverflow./q/6596294/562296 – milind Commented Jul 7, 2011 at 5:39
Add a ment  | 

3 Answers 3

Reset to default 4

It's possible, but I wouldn't do it for your given use case :) (the UI could be done with the Android framework)

You can add what they call "Javascript Interfaces", which are basically links from Javascript to Java code. First, define a class to bind containing the methods you which to call, and add it to your WebView :

    class HelloInterface  
    {  
        public void test(String name)  
        {  
            Log.i(TAG, "Hello " + name); 
        }  
    } 

    webview.addJavascriptInterface(new HelloInterface(), "HELLO");  

Then, from your javascript code, you can just call

window.HELLO.test("Webview");

Refer the following urls,

http://www.rgagnon./javadetails/java-0170.html

Post #6 of-> http://ubuntuforums/showthread.php?t=565819

Hope this helps..

its actually reall easy (and cool) use:

webView.addJavascriptInterface(js, "pageObject");

js is a java object made in you activty (or somewhere where you init the webview), pageObject is the namespace identifier (variable) you can use with javaScript on your webpage.

本文标签: is it posible to call java class from javascriptStack Overflow