从JavaScript中调用Android的功能

从JavaScript中调用Android的功能

本文介绍了从JavaScript中调用Android的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个HTML按钮,并通过JavaScript使从该按钮的呼叫机器人。我写了下面的code,这是不工作:我是初学者的机器人。

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


}

在javascript:

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

推荐答案

我回答我的问题,以便它可以帮助别人。:)终于解决了它: myWebView.setWebViewClient(新MyAppWebViewClient()); 这是缺少这样的JavaScript不能正常工作。我必须添加 @JavascriptInterface 作为Hardik特里维迪上述建议。这是工作的罚款了。

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.

这篇关于从JavaScript中调用Android的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:19