Phonegap Java调用Javascript函数

时间:2014-01-31 15:19:01

标签: java javascript android html cordova

我目前正在为Android制作一个hello world类型的Phonegap应用程序,其中我从本机android代码调用javascript函数来设置我的应用程序中的一个段落的文本。

我有一个javascript函数,我在我的应用程序的html中定义,我想在应用程序加载后从我的活动中调用它。该函数只是在我的应用程序中设置段落的文本。我无法让它发挥作用。

这是我的java活动代码:

public class MyPhoneGapActivity extends DroidGap {

    public DroidGap c;
    public LoadInfo info;

    @SuppressLint("SetJavaScriptEnabled") 
    @JavascriptInterface
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        c = this;
        appView.getSettings().setJavaScriptEnabled(true);
        appView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        appView.loadUrl("file:///android_asset/www/index.html");

        appView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                MyPhoneGapActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        c.sendJavascript("javascript:getMassTimes()");
                    }
                });
            }
        });
    }
}

这是我的javascript和html代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
    content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Ionic Seed App</title>
<link href="lib/css/ionic.css" rel="stylesheet">

<link href="css/app.css" rel="stylesheet">
<link href="lib/css/mycss.css" rel="stylesheet">
<script src="lib/js/ionic.js"></script>
<script src="lib/js/angular/angular.js"></script>
<script src="lib/js/angular/angular-animate.js"></script>
<script src="lib/js/angular/angular-sanitize.js"></script>
<script src="lib/js/angular-ui/angular-ui-router.js"></script>
<script src="lib/js/ionic-angular.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.js"></script>
</head>
<body ng-app="myApp">
    <script type="text/javascript">
        function getMassTimes() {
            document.getElementById("massTimes").textContent = "This is some text";
        }
    </script>
    <div>
        <header-bar title="Saint David the King"
            class="bar bar-header bar-energized">
        <h1 class="title">Saint David the King</h1>
        </header-bar>
        <br>
        <content scroll="true" has-header="true" width="100%" height="100%"
            id="content"> <img alt="Saint David the King"
            src="file:///android_asset/www/img/saintdavidthekingbackground2.png"
            width="100%" /> <br>
        <p id="massTimes">
        </p>
        </content>
    </div>
</body>
</html>

我做错了吗? 我见过这些网站:

Resolved - Call javascript function from Java

How to call javascript function from java code

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/1o9PgfmyRd8

http://www.jumpbyte.com/2012/phonegap-native-to-javascript/

Javascript calls from Android using PhoneGap

how to call javascript function from android using phonegap plugin

我已经尝试过他们对他们的所作所为,但我无法让它发挥作用。

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

c.sendJavascript("javascript:getMassTimes()");替换为 myBrowser.loadUrl("javascript:getMassTimes()");

这适用于我的问题。

如果要将String从活动传递给javascript使用

String msgToSend = "Hello World!"; myBrowser.loadUrl("javascript:callFromActivity(\""+msgToSend+"\")");