简体   繁体   English

Phonegap Java调用Javascript函数

[英]Phonegap Java call Javascript Function

I am currently making a hello world type Phonegap application for android, In which I call a javascript function from the native android code to set the text of one of the paragraphs in my app. 我目前正在为Android开发一个hello世界类型的Phonegap应用程序,其中我从本地android代码中调用了javascript函数来设置我应用程序中段落之一的文本。

I have a javascript function that I define in my app's html, and I want to call it from my activity once the app loads up. 我有一个在应用程序的html中定义的javascript函数,应用程序加载后,我想从我的活动中调用它。 The function just sets the text of a paragraph in my app. 该功能只是在我的应用程序中设置段落的文本。 I cannot get this to work. 我无法使它正常工作。

Here is my java activity code: 这是我的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()");
                    }
                });
            }
        });
    }
}

and here is my javascript and html code: 这是我的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>

Is there something I am doing wrong? 我做错什么了吗? I have seen these sites: 我看过这些网站:

Resolved - Call javascript function from Java 解决-从Java调用javascript函数

How to call javascript function from java code 如何从Java代码调用JavaScript函数

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

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

Javascript calls from Android using PhoneGap 使用PhoneGap从Android进行Java语言调用

how to call javascript function from android using phonegap plugin 如何使用phonegap插件从android调用javascript函数

And I have tried what they have on them, but I just can't get this to work. 我已经尝试过使用它们,但是我无法使它正常工作。

Any help appreciated! 任何帮助表示赞赏!

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

This worked for my problem. 这解决了我的问题。

If you want to pass a String from the activity to javascript use 如果要将字符串从活动传递给javascript,请使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM