关闭

Android webview加载Html页面,传参到Html并获取返回值

标签: Androidhtmlwebview与web端交互
4966人阅读 评论(0) 收藏 举报
分类:

1.Android端代码

public class MainActivity extends Activity {

private static final String LOGTAG = "MainActivity";
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final WebView myWebView = (WebView) findViewById(R.id.myWebView);

    WebSettings settings = myWebView.getSettings();
    settings.setJavaScriptEnabled(true);//设置JS可用
    myWebView.addJavascriptInterface(new JsInteration(), "control");//传递对象进行交互
    myWebView.setWebChromeClient(new WebChromeClient() {});
    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {//当页面加载完成
            super.onPageFinished(view, url);            


           if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){//当Android SDK>=4.4时
               callEvaluateJavascript(myWebView);
           }else {
            callMethod(myWebView);
           }
      }

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

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

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

   // call = "javascript:toastMessage(\"Hello World\")";

    //call = "javascript:sumToJava(1,2)";

    //call = "javascript:mult1(3,3)";

    webView.loadUrl(call);

}

public class JsInteration {

    @JavascriptInterface
    public void toastMessage(String message) {

        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }

    @JavascriptInterface
    public void onSumResult(int result) {

        System.out.println(LOGTAG+"_onSumResult result=" + result);
    }

    @JavascriptInterface
    public void onMultResult(int result) {

        System.out.println(LOGTAG+"_onMultResult result=" + result);
    }
}

private void callEvaluateJavascript(WebView webView) {

      webView.evaluateJavascript("mult2(3,3)", new ValueCallback<String>() {

      @Override
      public void onReceiveValue(String value) {         

          System.out.println(LOGTAG+"_onReceiveValue value=" + value);
          Toast.makeText(MainActivity.this, "onReceiveValue value=" + value, Toast.LENGTH_LONG).show();
      }});
    }

}

2.Html页面

<html> <script type="text/javascript">   
       function sayHello() {   alert("Hello")    }
       function alertMessage(message) {   alert(message)    }
       function toastMessage(message) 
       {  window.control.toastMessage(message)    }
       function sumToJava(number1, number2){
         window.control.onSumResult(number1 + number2)    }  
       function mult1(x,y) {
        window.control.onMultResult(x*y)
       }     
       function mult2(x,y) {
        return  x*y;
       }     
      function sub(x,y) {
       return  x-y;
       }
 <body> <center> <p>Android与Javascript之间的交互.</p> </center> </body>
  </html> 

3.运行结果
这里写图片描述
4.Demo下载地址
http://download.csdn.net/detail/sanyang730/9535274

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44482次
    • 积分:600
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:2篇
    • 译文:0篇
    • 评论:17条
    最新评论