Thursday, June 30, 2011

[android-developers] Re: How to bind multi-layer Java class to JavaScript by using addJavascriptInterface() on WebView

Thanks Mark's reply.

Now I'm sure that I can't bind a "multi-layer" java class to
javascript by using addJavascriptInterface()

I got another plan : I create a js file to define and create the
"multi-layer" class and using java to create function only.
This is a sample:
====demo.js begin====
function AClass() {
this.B = new Object();
this.B.func = function() { return JavaA.func(); }
A = new AClass();

====demo.html begin====
<script type="text/javascript" src="demo.js"></script>
<script type="text/javascript">
document.write("A.B.func1() return " + A.B.func());
====demo.html end==== begin====
public class MyDemo extends Activity {
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
mWebView = (WebView)findViewById(;
mWebView.addJavascriptInterface(new AClass(), "JavaA");
final class AClass {
AClass() {}
public int func() { return 1; }
} end====

It could work fine.

But then I found another question: I'm trying to create an app to load
a Company's Web page. There are some self-define javascript class and
interface on the target page and I should let my app to support them.
I make a xxx.js to define all of them. The I aware I can't modify the
Web page's source document, so I can't add "<script type="text/
javascript" src="xxx.js"></script>" to the <head> block of html
document. That means I should add and excute my javascript define(such
as the "demo.js" below) after loadUrl().

Can anyone tell me which android interface could help me?
If there are any function to load javascript code after

Thank you!

On 6月30日, 上午7时37分, Mark Murphy <> wrote:
> On Wed, Jun 29, 2011 at 8:06 AM, marco li <> wrote:
> >  So my question is:
> >  1. if I want to bind javascript interface format as "A.B.func()" to
> > my java class, how can I do it?
> I am not aware that this is possible.
> >  2. if I want to get javascript class's property directly, not by
> > function calling, how can I do it?
> >     a javascript sample to show Q2
> >     use "var prop ="
> >     not use "var prop = A.getProperty()"
> I am not aware that this is possible.
> --
> Mark Murphy (a Commons Guy)||
> _The Busy Coder's Guide to *Advanced* Android Development_ Version
> 1.9.9 Available!

You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment