android js調(diào)用java方法
ajax調(diào)用java后臺的方法,其實(shí)是通過url鏈接來訪問。下面是學(xué)習(xí)啦小編為大家?guī)硪黄猘jax調(diào)用java方法,希望對大家有所幫助。
android js調(diào)用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端調(diào)用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端調(diào)用webview的JS
}
});
JSInterface2.setWvClientClickListener(new webviewClick());//這里就是js調(diào)用java端的具體實(shí)現(xiàn)
}
}
class webviewClick implements wvClientClickListener {
@Override
public void wvHasClickEnvent() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "link be on click", 1000).show();
}
}
}
android js調(diào)用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設(shè)置
web.getSettings().setJavaScriptEnabled(true);
//把RIAExample的一個實(shí)例添加到j(luò)s的全局對象window中
//這樣就可以使用window.javatojs來調(diào)用它的方法
web.addJavascriptInterface(this, "javatojs");
//加載網(wǎng)頁
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.....()進(jìn)行調(diào)用
* @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");//創(chuàng)建一個p標(biāo)簽,再建個textnode
tnode = document.createTextNode(jsdata);
pnode.appendChild(tnode);//p中加入數(shù)據(jù)
datalistdiv.appendChild(pnode);//div中鍵入新的p
}
}
script>
head>
<body onload="javascript:window.javatojs.Callfunction()">//調(diào)用java方法
<div id = "datalist">
this is a demo
div>
body>
html>