SSS
SSS

Reputation: 1430

call android function from javascript

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

Upvotes: 0

Views: 4900

Answers (3)

puneet dubey mudia
puneet dubey mudia

Reputation: 115

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

Upvotes: 0

SSS
SSS

Reputation: 1430

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.

Upvotes: 0

Hardik Trivedi
Hardik Trivedi

Reputation: 6102

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.

Upvotes: 3

Related Questions