js与java通信

时间:2022-12-07 15:36:19

js 调用java中的接口并传递参数给客户端处理方式:

webView.addJavascriptInterface(new NewsDetail() , "newsDetail");

protected final class NewsDetail {
  
  @JavascriptInterface
  public void getContent(String imgUrl,String lineLink,String descContent,String shareTitle) {
   Logger.d("获取到了新闻详情imgUrl=" + imgUrl);
   Logger.d("获取到了新闻详情lineLink=" + lineLink);
   Logger.d("获取到了新闻详情descContent=" + descContent);
   Logger.d("获取到了新闻详情shareTitle=" + shareTitle);
   name = shareTitle;
   content = descContent;
   webUrl = lineLink;
   shareImageUrl = imgUrl;
  }
 }

js调用:newsDetail.getContent(imgUrl,lineLink,descContent,shareTitle);

java调用js中的方法:webView.loadUrl(String.format("javascript:java3js()"));