如何从 android 片段调用 javascript 函数?

How to call a javascript function from android fragment?

我在一个片段中有一个 webview 并且有我自己的 Bridgecallback 服务。

我有一个javascript文件TriggerEventsService.js

define(function(require){

    var Service = require('BridgeService');
    var TriggerEventsService = new Service({
        name: "TriggerEventsService",
        timeout : 3000
    });

    //Public API
    TriggerEventsService.postHibernate = function(){
        this.execute("postHibernate", {"event" : "App becoming inactive"}, function(){}, function(){});
    };

在我的HTML文件中index.html

  function postEventHibernate(){
            require(['TriggerEventsService'], function(ui){
                ui.postHibernate();
            });
        }

我可以直接从本机 android 方法调用此函数吗?

iOS中我这样做了:

    NSString *jsString = [NSString stringWithFormat:@"require(['TriggerEventsService'],
 function(ui){ui.postHibernate();});"];
             [self.webView evaluateJavaScript:jsString completionHandler:nil];

工作正常!我在iOS知道一个方法,想知道任何人都可以帮助我如果 android 有任何直接方法 ?

我看过其他答案,但不确定如何调用我的 TriggerEventsService.js - webview.loadUrl("究竟应该做什么我在这里打电话?");

I have seen other Answers but was not sure how to call the my TriggerEventsService.js - webview.loadUrl("WHAT EXACTLY SHOULD I CALL HERE ?");

假设 postEventHibernate() 是一个全局 JavaScript 函数,这将是:

webview.loadUrl("javascript:postEventHibernate();");

FWIW,这在 my book, though most of this specific material is in the preview edition of my advanced WebView chapter.

中有介绍