0

I want to create a button in html and make a call from that button from android through javascript. I have written the following code , which is not working : I am beginner for android.

public class MainActivity extends Activity {

    private static final String PIC_WIDTH = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);

        myWebView.addJavascriptInterface(new JsInterface(), "android");  
myWebView.loadUrl("file:///android_asset/www/index.html");
}

public class JsInterface{  
        public void makeCall()
        {
            Log.i("Myactivity","inside android makecall");
            // Here call any of the public activity methods....
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:9611396958"));
            startActivity(callIntent);
        }
    }


}

in javascript :

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

</head>


    <body class="bgClass" id="body" ontouchstart="">

    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>   

    <script src="js/myScript.js"></script>
    <button id="makeCall" >CALL</button>
    <script>
      $("#makeCall").click(function(){
        console.log("inside click javascript");
        console.log(window.android);
        android.makeCall();
      });
      </script>
        </body>
</html>

any help??

SSS
  • 1,380
  • 3
  • 28
  • 48

3 Answers3

3

Make sure your $("#makeCall") method is responding in html.

Then make below changes.It will work.

public class JsInterface{ 
        @JavascriptInterface
        public void makeCall()
        {
            Log.i("Myactivity","inside android makecall");
            // Here call any of the public activity methods....
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:9611396958"));
            startActivity(callIntent);
        }
    }

This annotation allows exposing methods to JavaScript.

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51
  • If i do that it will give error as : JavascriptInterface cannot be resolved to a type – SSS Jan 10 '14 at 10:27
  • @JavascriptInterface is introduced in JellyBean. You need to set the Project Build Target to API Level 17 From Eclipse IDE, go to Project->Properties. Select Android and set the target The APK will work on older versions of android. – Hardik Trivedi Jan 10 '14 at 10:40
  • What this console.log("inside click javascript"); console.log(window.android); two lines print ? – Hardik Trivedi Jan 10 '14 at 10:50
  • it prints inside click javascript. Then tells window.android is not defined – SSS Jan 10 '14 at 10:51
  • As far as I know there is nothing like window.android. Please remove that ? For what purpose you have written that line ? – Hardik Trivedi Jan 10 '14 at 10:52
  • ok removed it... Still nothing happens. Is my android part code correct?? – SSS Jan 10 '14 at 10:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44942/discussion-between-hardik-trivedi-and-sss) – Hardik Trivedi Jan 10 '14 at 10:58
0

I am answering my own question so that it can help others .:) Finally solved it : myWebView.setWebViewClient(new MyAppWebViewClient()); it was missing so javascript was not working . And i must add @JavascriptInterface as Hardik Trivedi suggested above. It is working fine now.

SSS
  • 1,380
  • 3
  • 28
  • 48
0

simply adding webView.getSettings().setJavaScriptEnabled(true); worked for me. must add @JavascriptInterface too.