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.js.end====

====demo.html begin====
<html>
<head>
<script type="text/javascript" src="demo.js"></script>
</head>
<body>
<script type="text/javascript">
document.write("A.B.func1() return " + A.B.func());
</script>
</body>
</html>
====demo.html end====

====demo.java begin====
public class MyDemo extends Activity {
private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
...
mWebView = (WebView)findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new AClass(), "JavaA");
mWebView.loadUrl("file:///android_asset/demo.html");
}
final class AClass {
AClass() {}
public int func() { return 1; }
}
}
====demo.java 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
WebView.loadUrl()?

Thank you!

On 6月30日, 上午7时37分, Mark Murphy <mmur...@commonsware.com> wrote:
> On Wed, Jun 29, 2011 at 8:06 AM, marco li <marcoli872...@gmail.com> 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 = A.property"
> >     not use "var prop = A.getProperty()"
>
> I am not aware that this is possible.
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> _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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment