gpt4 book ai didi

Android - 从 Webview 调用 Java

转载 作者:行者123 更新时间:2023-11-29 01:54:24 25 4
gpt4 key购买 nike

我想从 Webview 调用 Java。

我在下面有 JavaScriptInterface:

class JavaScriptInterface{

private Activity activity;

public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}

public void open(String message){
//do something
}

}

我添加JavaScriptInterface

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

在我的 HTML 中,我有这个 onclick 事件:

onclick="window.JSInterface.open('hi');"

我也试过:

onclick="JSInterface.open('hi');"

我在 logcat 中有这个错误:

04-16 14:55:15.829: W/dalvikvm(721): JNI WARNING: jarray 0x4050cf68 points to non-array object (Ljava/lang/String;)
04-16 14:55:15.829: I/dalvikvm(721): "WebViewCoreThread" prio=5 tid=9 NATIVE
04-16 14:55:15.829: I/dalvikvm(721): | group="main" sCount=0 dsCount=0 obj=0x40521218 self=0x1f6cf8
04-16 14:55:15.829: I/dalvikvm(721): | sysTid=729 nice=0 sched=0/0 cgrp=default handle=2059824
04-16 14:55:15.839: I/dalvikvm(721): | schedstat=( 5717972154 3300872155 567 )
04-16 14:55:15.839: I/dalvikvm(721): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
04-16 14:55:15.839: I/dalvikvm(721): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
04-16 14:55:15.839: I/dalvikvm(721): at android.webkit.WebViewCore.access$3300(WebViewCore.java:53)
04-16 14:55:15.839: I/dalvikvm(721): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1158)
04-16 14:55:15.863: I/dalvikvm(721): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 14:55:15.869: I/dalvikvm(721): at android.os.Looper.loop(Looper.java:130)
04-16 14:55:15.869: I/dalvikvm(721): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
04-16 14:55:15.869: I/dalvikvm(721): at java.lang.Thread.run(Thread.java:1019)
04-16 14:55:15.869: E/dalvikvm(721): VM aborting

如何解决?或者其他方法?

谢谢

最佳答案

  1. 您需要通过设置在 WebView 中启用 Javascript

    final WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setVerticalScrollBarEnabled(false);
    myWebView.setHorizontalScrollBarEnabled(false);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  2. 您需要在 HTML DOM 中加载 JS。

    JsInterface jsInterface = new JsInterface();
    jsInterface.wordDef = content;
    myWebView.addJavascriptInterface(jsInterface, "interfaces");
    myWebView.loadUrl("file:///android_asset/some.html");
  3. 从 HTML/JS 调用您的界面

    /**JAVA CODE **/
    public class JsInterface {
    public String someString;
    Context mContext;

    public void JsInterface(Context c){
    mContext = c;
    }

    public String someFunction(){
    Log.v("JSInterface", ""+someString);
    return someString;
    }
    }

    /* JS Code */
    function addData(){
    var data = interfaces.someFunction();
    }

关于Android - 从 Webview 调用 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16030737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com