Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android call Javascript from Java not working

I've read many posts but I can't get my Android app call a Javascript function. This is what I have

public class BorrameActivity extends AppCompatActivity {

    private static BorrameActivity.MyJavaScriptInterface myJavaScriptInterface;
    private static WebView webView;

    public class MyJavaScriptInterface {
        Context mContext;

        MyJavaScriptInterface(Context c) {
            mContext = c;
        }

        @android.webkit.JavascriptInterface
        public void doEchoTest(String echo){
            Log.e("printer", echo);
            Toast toast = Toast.makeText(mContext, echo, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_borrame);

        myJavaScriptInterface = new BorrameActivity.MyJavaScriptInterface(this);
        webView = (WebView) findViewById(R.id.mybrowserAuthorise);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(myJavaScriptInterface, "JSInterface");
        webView.loadUrl("file:///android_asset/loginProtocol.html");
        //webView.loadUrl("javascript:JSInterface.doEchoTest('test')"); // THIS WORKS!!
        webView.loadUrl("javascript:testEcho()"); // THIS DOES NOT WORK
    }
}

This works when I execute

webView.loadUrl("javascript:JSInterface.doEchoTest('test')");

But it doesn't work when the Javascript function is in loginProtocol.html:

webView.loadUrl("javascript:testEcho()");

Where

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width; user-scalable=0;" />
    <title></title>
</head>
<body style="visibility:visible">
    <!--    Used for debugging -->
    <h1>HELLO</h1>

    <script>
        function testEcho(){
               JSInterface.doEchoTest('This does not get printed in Android Log');
        }
    </script>
</body>
</html>

and

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sedicii.app.sedicii.BorrameActivity">

    <WebView
        android:id="@+id/mybrowserAuthorise"
        android:layout_width="353dp"
        android:layout_height="442dp"/>

</LinearLayout>

The WebView seems to be loading correctly (I can see the HELLO from loginProtocol.html on the Android screen).

What am I missing?

SOLUTION I have to wait until the HTML page is loaded. Thanks @KosWarm

webView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        webView.loadUrl("javascript:testEcho()");
    }
});
like image 488
Mike Avatar asked May 27 '26 16:05

Mike


1 Answers

I think you just need to give parameter to your loadUrl method:

webView.loadUrl("javascript:testEcho('Hello World!')");

and your javascript function must look like:

function testEcho(testHello){
 window.JSInterface.doEchoTest(testHello);
}
like image 68
Amin Memariani Avatar answered May 30 '26 05:05

Amin Memariani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!