学习啦>知识大全>方法百科>产品使用方法>

android js调用java方法

时间: 陈哲凡673 分享

  ajax调用java后台的方法,其实是通过url链接来访问。下面是学习啦小编为大家带来一篇ajax调用java方法,希望对大家有所帮助。

  android js调用java方法(一)

  [java] view plaincopypublic class JsInterface {

  /*interface for javascript to invokes*/

  public interface wvClientClickListener {

  public void wvHasClickEnvent();

  }

  private wvClientClickListener wvEnventPro = null;

  public void setWvClientClickListener(wvClientClickListener listener) {

  wvEnventPro = listener;

  }

  public void javaFunction() {

  if(wvEnventPro != null)

  wvEnventPro.wvHasClickEnvent();

  }

  }

  /*Js2JavaActivity.JAVA*/

  /*程序入口*/

  [java] view plaincopypublic class Js2JavaActivity extends Activity{

  private Button btn_show,btn_hide;

  private WebView wv;

  private JsInterface JSInterface2 = new JsInterface();

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  // TODO Auto-generated method stub

  super.onCreate(savedInstanceState);

  setContentView(R.layout.js2java);

  btn_show = (Button)findViewById(R.id.btn_java2js_show);

  btn_hide = (Button)findViewById(R.id.btn_java2js_hide);

  wv = (WebView)findViewById(R.id.wv_js2java);

  wv.getSettings().setJavaScriptEnabled(true);

  wv.addJavascriptInterface(JSInterface2,"JSInterface2");

  wv.setWebViewClient(new webviewClient());

  wv.loadUrl("file:///android_asset/index.html");

  }

  class webviewClient extends WebViewClient {

  @Override

  public void onPageFinished(WebView view, String url) {

  // TODO Auto-generated method stub

  super.onPageFinished(view, url);

  btn_show.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  Toast.makeText(getBaseContext(), "You click show button", 1000).show();

  wv.loadUrl(String.format("javascript:java2js(0)"));//这里是java端调用webview的JS

  }

  });

  btn_hide.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  Toast.makeText(getBaseContext(), "You click hide button", 1000).show();

  wv.loadUrl(String.format("javascript:java2js(1)"));//这里是java端调用webview的JS

  }

  });

  JSInterface2.setWvClientClickListener(new webviewClick());//这里就是js调用java端的具体实现

  }

  }

  class webviewClick implements wvClientClickListener {

  @Override

  public void wvHasClickEnvent() {

  // TODO Auto-generated method stub

  Toast.makeText(getBaseContext(), "link be on click", 1000).show();

  }

  }

  }

  android js调用java方法(二)

  JavaJsDemo.java

  package com.demo.android.JavaJsDemo;

  import android.app.Activity;

  import android.os.Bundle;

  import android.webkit.WebView;

  import java.util.ArrayList;

  import java.util.List;

  public class JavaJsDemo extends Activity {

  /** Called when the activity is first created. */

  private WebView web;

  public List<String> list;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  initData();

  setContentView(R.layout.main);

  web = (WebView)this.findViewById(R.id.webview);

  //开启javascript设置

  web.getSettings().setJavaScriptEnabled(true);

  //把RIAExample的一个实例添加到js的全局对象window中

  //这样就可以使用window.javatojs来调用它的方法

  web.addJavascriptInterface(this, "javatojs");

  //加载网页

  web.loadUrl("file:///android_asset/index.html");

  }

  void initData(){

  list=new ArrayList<String>();

  for(int i=0;i<5;i++){

  list.add("我是List中的第"+(i+1)+"行");

  }

  }

  /**

  * 该方法将在js脚本中,通过window.javatojs.....()进行调用

  * @return

  */

  public Object getObject(int index){

  return list.get(index);

  }

  public int getSize(){

  return list.size();

  }

  public void Callfunction(){

  web.loadUrl("javascript: GetList()");

  }

  }

  index.html

  DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://xxx.w3.org/TR/html4/loose.dtd">

  <html>

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  <title>demotitle>

  <script language="javascript">

  function GetList(){

  var i=window.javatojs.getSize();

  for(var n=0;n<i;n++){

  var jsdata= window.javatojs.getObject(n);//拿到activity里面的属性javadata

  var datalistdiv = document.getElementById("datalist"); //得到页面的div

  pnode = document.createElement("p");//创建一个p标签,再建个textnode

  tnode = document.createTextNode(jsdata);

  pnode.appendChild(tnode);//p中加入数据

  datalistdiv.appendChild(pnode);//div中键入新的p

  }

  }

  script>

  head>

  <body onload="javascript:window.javatojs.Callfunction()">//调用java方法

  <div id = "datalist">

  this is a demo

  div>

  body>

  html>

android js调用java方法

ajax调用java后台的方法,其实是通过url链接来访问。下面是学习啦小编为大家带来一篇ajax调用java方法,希望对大家有所帮助。 android js调用java方法(一) [java] view plaincopypublic class JsInterface { /*interface for javascript
推荐度:
点击下载文档文档为doc格式
374453