admin管理员组文章数量:1391793
Im trying to invoke a method in java from javascript, but this doesn't happen when I run the application in the emulator, the application stops when it is suppose to call the method in java.
here is the java code:
import android.os.Bundle;
import android.webkit.WebView;
import .phonegap.*;
public class App extends DroidGap {
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.addJavascriptInterface(new message(), "Show");
super.loadUrl("file:///android_asset/www/index.html");
}
class message {
String msg() {
return "Hello World!!";
}
}
}
here is the javascript:
<script type="text/javascript">
{
alert("Start");
alert(Show.msg());
alert("End");
}
</script>
It shows the first alert but nothing thereafter, can anyone help?
Im trying to invoke a method in java from javascript, but this doesn't happen when I run the application in the emulator, the application stops when it is suppose to call the method in java.
here is the java code:
import android.os.Bundle;
import android.webkit.WebView;
import .phonegap.*;
public class App extends DroidGap {
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.addJavascriptInterface(new message(), "Show");
super.loadUrl("file:///android_asset/www/index.html");
}
class message {
String msg() {
return "Hello World!!";
}
}
}
here is the javascript:
<script type="text/javascript">
{
alert("Start");
alert(Show.msg());
alert("End");
}
</script>
It shows the first alert but nothing thereafter, can anyone help?
Share Improve this question edited May 7, 2011 at 19:09 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked May 7, 2011 at 18:57 Thabo MphuthiThabo Mphuthi 631 silver badge5 bronze badges2 Answers
Reset to default 4Your problem is that you're half using PhoneGap and half not. You're creating a separate WebView
class from PhoneGap's. The WebView class that you added "Show" to never gets used. Instead the WebView class that is a member of the super
(DroidGap
) is.
You should do one of two things.
- Use PhoneGap's plugin structure (see examples here)
Don't use PhoneGap at all and have a class that looks more like the following:
public class act extends Activity { WebView webView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); webView = new WebView(this); webView.getSettings().setJavaScriptEnabled(true); // Set JS alert() hook webView.setWebChromeClient(new WebChromeClient() { public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return false; } }); webView.loadUrl("file:///android_asset/www/index.html"); // Add JS libraries webView.addJavascriptInterface(new message(), "Show"); } class message { public String msg() { return "Hello World!!"; } } }
Note that the method msg
needs to be public
Why not just use AlertDialog?
private void showDialog(int title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
本文标签: Binding JavaScript code to Android codeStack Overflow
版权声明:本文标题:Binding JavaScript code to Android code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744765400a2624008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论