package com.cky.learnandroiddetails.webviewandh5;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.cky.learnandroiddetails.R;

public class WebViewAndH5 extends AppCompatActivity {

    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.web_view);

        WebSettings mWebViewSettings = mWebView.getSettings();

        mWebViewSettings.setJavaScriptEnabled(true);

        mWebView.addJavascriptInterface(new JsInteration(), "control");
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //testMethod(mWebView);
            }
        });

        mWebView.loadUrl("file:///android_asset/js_java_interaction.html");
    }

    private void testMethod(WebView webView) {
        String call = "javascript:sayHello()";

        call = "javascript:alertMessage(\"" + "content" + "\")";

        call = "javascript:toastMessage(\"" + "content" + "\")";

        call = "javascript:sumToJava(1,2)";
        webView.loadUrl(call);

    }

    public class JsInteration {

        @JavascriptInterface
        public void toastMessage(String message) {
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
        }

        @JavascriptInterface
        public void onSumResult(int result) {
            Toast.makeText(getApplicationContext(), "onSumResult result=" + result, Toast.LENGTH_LONG).show();
        }
    }
}

@@ -1153,18 +1153,16 @@
         
-//
 testMeth
@@ -1380,32 +1380,34 @@
 lo()%22;%0A%0A        
+//
 call = %22javascri
@@ -1421,39 +1421,44 @@
 tMessage(%5C%22%22 + %22
-content
+%E6%88%91%E6%98%AF%E5%8E%9F%E7%94%9F%E4%BC%A0%E9%80%92%E5%88%B0H5%E7%9A%84%E5%8F%82%E6%95%B0
 %22 + %22%5C%22)%22;%0A%0A    
@@ -1453,32 +1453,34 @@
 %22%5C%22)%22;%0A%0A        
+//
 call = %22javascri
@@ -1506,15 +1506,26 @@
  + %22
-content
+%E6%88%91%E5%85%88%E4%BB%8E%E5%8E%9F%E7%94%9F%E5%88%B0H5,%E5%86%8D%E4%BB%8EH5%E4%BC%A0%E9%80%92%E5%88%B0%E5%8E%9F%E7%94%9F
 %22 + 

更新【WebView JS 交互】相关