有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从Java到Webview Javascript的Android回调函数

我试图实现一个简单的回调函数,从Java调用函数到WebView。我成功地将该函数从Javascript调用到Java,但另一方面却没有成功

这是我的主要活动。java

public class MainActivity extends AppCompatActivity {

WebView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    view= new WebView(this);
    setContentView(view);

    view.loadUrl("file:///安卓_res/raw/meter.html");
    view.getSettings().setJavaScriptEnabled(true);
    view.addJavascriptInterface(new WebAppInterface(this), "Android");

}

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }
    /** Show a toast from the web page */
    @JavascriptInterface
    public void register(String callback) {
        Log.e("work","log printing");   // <--- coming here
        view.loadUrl("javascript:callbacker('hola moma');");
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
}

这是我的表。html文件

    <script type="text/javascript">
        function showAndroidToast(toast) {
            Android.register("callbacker");
        }
        function callbacker(data){
            Android.showToast(data);
        }
    </script>
</head>
<body>

     <p>Display a gauge:</p>
    <meter value="2" min="0" max="10">2 out of 10</meter><br>
    <meter value="0.6">60%</meter>
     <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
    <p><strong>Note:</strong> The &lt;meter&gt; tag is not supported in Internet Explorer, Edge 12, Safari 5 and earlier versions.</p>

</body>

现在,当我运行应用程序时,MainActivity的register函数中的Log。java是打印出来的。但下一行是调用回调函数,而不是调用。有什么问题吗


共 (0) 个答案