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

11
  • You should take a look at Cordova and Sencha frameworks... Commented Jan 10, 2014 at 9:17
  • What does "not working" mean, are you getting error's in the console, is it doing anything, are print out's saying its getting so far etc. Commented Jan 10, 2014 at 9:18
  • when i click button nothing happens. It says android is not defined Commented Jan 10, 2014 at 9:19
  • @SSS please post your full index.html , I am already in solving stage. so please post full html page from which this js called Commented Jan 10, 2014 at 9:39
  • I posted full index.html. see the edited code Commented Jan 10, 2014 at 9:41

3 Answers 3

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.

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

7 Comments

If i do that it will give error as : JavascriptInterface cannot be resolved to a type
@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.
What this console.log("inside click javascript"); console.log(window.android); two lines print ?
it prints inside click javascript. Then tells window.android is not defined
As far as I know there is nothing like window.android. Please remove that ? For what purpose you have written that line ?
|
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.

Comments

0

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

Comments

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.