Java and JS interaction

Java tuning JS

1.loadUrl

2. EvaluateJavascript (> = anroid4.4)

JS tuning Java

1.addJavascriptInterface

Project code

HTML file in/SRC/main/assets/testJs HTML

<html>
<head>
    <title>Js calls native Android code</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta id="viewport" name="viewport"
          content="Width = device - width, initial - scale = 1.0, the minimum - scale = 1.0, the maximum - scale = 1.0, minimal - UI">
    <script type="text/javascript">
        function javaCallJsNoParams(){
             document.getElementById("content").innerHTML +=
                 "
       

Java call JS with no arguments" \>
; } <! Select * from android; select * from Android;function javaCallJsWithParams(data){ document.getElementById("content").innerHTML += ("

Java call js parameter:" \>
+data); }
</script> </head> <body> <br/><br/> <li><a onClick="window.testCalljava.jsCallJavaNoParams()">Click to invoke the Java code</a></li> <! -- You can transfer the data from android terminal, after processing, put it here and then pass it to Android terminal --> <li><a onClick="Window. TestCalljava. JsCallJavaWithParams (' I'm coming web data)">Click to invoke the Java code and pass the parameters</a></li> <br/> <div id="content">content</div> </body> </html> Copy the code

Java code

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.my_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/testJs.html");
        mWebView.addJavascriptInterface(new JsCallJavaInterface(this), "testCalljava");
    }


    public void callJsNoParam(View view) {
        String jsString = "javascript:javaCallJsNoParams()";
// mWebView.loadUrl(jsString);
        mWebView.evaluateJavascript(jsString, null);EvaluateJavascript is available for high efficiency
    }

    public void callJsWithParam(View view) {
        mWebView.loadUrl("javascript:javaCallJsWithParams('" + "Parameters from Android" + "')");
    }

    class JsCallJavaInterface {
        private Context mContext;

        public JsCallJavaInterface(Context mContext) {
            this.mContext = mContext;
        }

        @JavascriptInterface
        public void jsCallJavaNoParams(a) {
            Toast.makeText(mContext, "Js calls Android with no arguments", Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void jsCallJavaWithParams(String data) {
            Toast.makeText(mContext, "Js calls Android pass:"+ data, Toast.LENGTH_SHORT).show(); }}}Copy the code

Layout file


      
<RelativeLayout 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=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#00ffff"
        android:gravity="center"
        android:text="Web Test"
        android:textColor="#ffffff"
        android:textSize="30sp" />

    <WebView
        android:id="@+id/my_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp" />

    <Button
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:onClick="callJsNoParam"
        android:text="Call JS with no arguments" />

    <Button
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:onClick="callJsWithParam"
        android:text="Call JS with parameters" />

</RelativeLayout>
Copy the code