如何从本机 Java 代码调用 JS

How to call JS from native Java code?

本文关键字:代码 调用 JS Java 本机      更新时间:2023-09-26

如何使用 Cordova 在原生 Android 代码中调用一个简单的事件或执行 JavaScript 函数?

我试过了

    public void notificationRecieved(String heading,String news){
        try{
            if(webview!=null){
                Log.i("PUSHPARSE","in Notification Received Event");
                //webview.loadUrl("javascript:alert('URLcall')");
                webview.sendJavascript("alert('CAlling from Java')");
            }   
        }catch(Exception e1){
            Log.e("PUSHPARSE",e1.getMessage(),e1);
        }
    }

我无法收到任何通知或警报。

更新

Logcat显示I/PUSHPARSE( 2039): in Notification Received Event

尝试做 webview.loadUrl("http://www.google.com");

日志显示

>>> loadUrl(http://www.google.com)
W/CordovaWebView( 2039): CordovaWebView.init() was not called. This will soon be required.
D/CordovaWebView( 2039): CordovaWebView is running on device made by: unknown

我想警报在网络视图中不起作用,请尝试 document.write 或控制台.log以查看

谢谢

也许你可以使用 pushPlugin https://github.com/phonegap-build/PushPlugin 并使用方法函数 onNotification(e) ?