无法在Android中调用@JavaScriptInterface方法

时间:2014-07-19 22:42:48

标签: javascript android webview android-webview

我正在尝试将事件数据从我的网页视图传输到我的Android应用程序。不幸的是,由于某种原因,Javascript接口方法永远不会被调用。我的部分HTML是AJAX调用的响应。我只是要显示带有注释的HTML,其中的信息来自服务器。这是我的HTML:

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<link rel="stylesheet" href="styling.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script>
lastRecord=0;

    function loadEvents(){

        $('#sample').html( 'hello' );
        $.get(
        "eventquery.php?lastRecord="+lastRecord,
        function( data ) {
            $('#todayCollapsible').append( data )
            .trigger( 'create' )    
            .listview( 'refresh' );
            }); 
    }
</script>
</head>
<body style="background-color : #e9e9e9;" onload="loadEvents()">

<div data-iconpos="none" data-role="collapsibleset" data-theme="a" data-content-theme="a">

<div data-role="collapsible" data-collapsed="false">
    <h2><img src="today_calendar.png" style="height: 60px; vertical-align:middle;"> Today's Events</h2>
    <div id="todayCollapsible">
    <!-- Load from php using ajax call -->
    <div id="'.$row['Event ID'].'" class="event-wrapper" data-role="collapsible" data-collapsed="true">
    <h5 class="title" onclick="addEvent()">'.$row['Title'].'</h5>
    <ul data-role="listview" id="today_events">
    <li class="event"><a href="#">
    <table><tr><td>Date</td>
    <td class="date" style="padding: 10px;">.$row['Date'].</td>'
    </tr><tr>
    <td> Time </td>
    <td class="time" style="padding: 10px;">.$row['Time_Duration'].</td>
    </tr><tr>
    <td> Venue </td>
    <td class="venue" style="padding: 10px;">.$row['Venue'].</td>
    </tr></table></a></li>
    <li style="background-color: #CADECC;">
    <button class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-calendar" style="width: 170px; float: right; position: relative;">Add to calendar</button>
    </li></ul>
    </div>
    <!-- php data finished -->
    </div>
    </div>
</div>
<script>
var title;
var date;
var time;
var venue;

    $("body").on('click','button',function(){
        title = $(this).closest(".event-wrapper").find(".title").text();
        date =  $(this).closest(".event-wrapper").find("table .date").text();
        time =  $(this).closest(".event-wrapper").find("table .time").text();
        venue = $(this).closest(".event-wrapper").find("table .venue").text();

    alert(title+date+time+venue);
    console.log("executing function");
    Android.addCalendarEvent(title,date,time,venue);
    console.log("function executed");
    });
</script>
</body>
</html>

EventActivity.java

private WebView myWebView;
private String webaddress = "http://.....";

protected void onCreate(Bundle savedInstance){
...
myWebView.getSettings().setJavaScriptEnabled(true);

myWebView.addJavascriptInterface(this,"Android");

myWebView.loadUrl(webaddress);
}

@JavascriptInterface
public void addCalendarEvent(String title, String date, String time, String venue){

//Toast.makeText(this,title+date+time+venue,Toast.LENGTH_LONG).show();
Log.d("Data from JS", title+date+time+venue);

}

1 个答案:

答案 0 :(得分:0)

JavaInterface方法不能在UI线程上运行。

尝试像这样结束你的内心方法

@JavascriptInterface
public void addCalendarEvent(String title, String date, String time, String venue){
           mActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Log.d("Data from JS", title+date+time+venue);
                }

            });
}

同时检查此question