4

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 com.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?

2 Answers 2

4

Your 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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I see what you mean... So would it make sense for me to replace the message class with a tcp\ip client and have it communicate with a server on a desktop computer? I intend on using wifi for this and I have already tested the connection using a telnet application on the android device, the communication works perfectly.
I believe so. You should be able to put any Android native code you want in the message class.
0

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();
}

1 Comment

I only use the alert(String) method in the example to check if the interaction between the java and javascript is working, In the application I’m writing I'll want to call different methods each performing different actions and returning different types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.