Search code examples
javajavascriptandroidinterfacebridge

calling java function from javascript on android


I'm trying to make connection between javascript and java in android app and i'm able to control javascript in a way that it fills "gsm_num" field with 111111 and later with 222222 but it cannot call a function "jsmessage" in java...i tried moving @SuppressLint("JavascriptInterface") before whole on click function but it just can't print js message... can someone please show me what am i doing wrong?

@Override
        public void onClick(View arg0) {

            String url = "http://www.page.com";
            wv.loadUrl(url);
            setContentView(wv);
            wv.getSettings().setJavaScriptEnabled(true);
            wv.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view1, String url) {

                    view1.getSettings().setJavaScriptEnabled(true);
                    view1.loadUrl("javascript: document.getElementById('gsm_num').value = '11111111'; document.forms['button'].submit();");

                    view1.setWebViewClient(new WebViewClient() {
                        @SuppressLint("JavascriptInterface")
                        @JavascriptInterface
                        public void onPageFinished(WebView view2, String url) {

                            final class JavaScriptInterface {
                                JavaScriptInterface () { }
                                public void jsmessage() {
                                    Log.e("btijs", "message");
                                }
                              }


                            view2.getSettings().setJavaScriptEnabled(true);

                            view2.addJavascriptInterface(new JavaScriptInterface(), "JSInterface");
                            view1.loadUrl("javascript: document.getElementById('gsm_num2').value = '2222222'; window.JSInterface.jsmessage();");


                         }

                    });
                }
            });
        }
    });

Solution

  • I think there are a couple problems here. First I think you need to add a new JavaScriptInterface to your WebView like this:

        wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    

    And I think your JavaScript code itself might not work. You're not actually declaring your input as a function. Here's some code I used to do a similar task:

        wv.loadUrl("javascript:(function() { " +
                            "window.HTMLOUT.showHTML(document.getElementById('role').innerHTML);" +
                            "})()");
    

    Where showHTML is a method I wrote inside the JavaScriptInterface

    Here is my JavaScriptInterface class inside of the method where I'm using it:

    private void Authentication(){
    
        class MyJavaScriptInterface {
    
            @JavascriptInterface
            public void showHTML(String content) {
                // grants access based on authorization level
    
                }
            }
        }
    
        // open up the login page
        final WebView wv = (WebView)findViewById(R.id.login_webview);
    
        wv.getSettings().setJavaScriptEnabled(true);
    
        wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
    
                //once page is finished loading, check id="role" pass that value to showHTML
                if(url.contains(getString(R.string.loginURL))) {
                    wv.loadUrl("javascript:(function() { " +
                            "window.HTMLOUT.showHTML(document.getElementById('role').innerHTML);" +
                            "})()");
    
                }
    
            }
    
            @Override
            public void onReceivedError(WebView view, int errorCode, String description,
                                        String failingUrl) {
                Log.w("LoginActivity: ", description);
            }
        });
    
        wv.loadUrl(getString(R.string.loginURL));
    
    }