can not call a java method from javascript using addJavascri

Put your problem here if it does not fit any of the other categories.

can not call a java method from javascript using addJavascri

Postby sherwin » Tue Mar 17, 2009 7:04 am

hi
I'm trying to call a java method from javascript using addJavascriptInterface();
but seems does not work, it always display "failure";
java code:
public class WebTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.setClickable(true);
TestJs ts = new TestJs();
webview.addJavascriptInterface(ts, "test");


webview.loadUrl("file:///data/1.html");


setContentView(webview);

}
}
public class TestJs {


public int incr(int num){
return num+1;
}
}


html:
<html>
<head>
<script type="text/javascript">
function loader() {
document.write("begin");
document.write("<p>");
document.write(window.test);
document.write("<p>");
if(window.test){
document.write("success");
document.write("<p>");
document.write(test.incr(100));
}else{
document.write("failure");
document.write("<p>");
}


}
</script>
</head>
<body onload="loader()">
</body>
</html>


Thanks in advance for your help![syntax="java"]
sherwin
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Feb 09, 2009 7:24 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 14 guests