无法在Android中调用@JavaScriptInterface方法

Unable to call @JavaScriptInterface method in Android

本文关键字:调用 @JavaScriptInterface 方法 Android      更新时间:2023-09-26

我正试图将事件数据从我的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);
}

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

试着像这样包装你的innermethod

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

也检查这个问题