Android WebView中使用Java调用JavaScript方法并获取返回值

package com.financial.activity;




import java.util.Iterator;


import org.json.JSONObject;






import com.financial.R;
import com.financial.bll.AccountBLL;
import com.financial.fund.widget.ScreenManager;
import com.kit.CallBack;
import com.loopj.android.http.RequestParams;


import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;



public class WeBActivity extends Activity implements OnClickListener{
private WebView web;
/** @Fields retBtn : 返回按钮*/ 
private ImageView retBtn;
private AccountBLL bll;
private Handler handler;
private String path="";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web_activity);
ScreenManager.getInstance().addActivity(this);
bll=new AccountBLL(this);
web=(WebView) findViewById(R.id.web);
// web.loadUrl("www.aviationmaterial.com");
retBtn=(ImageView) findViewById(R.id.backbtn);
retBtn.setOnClickListener(this);


getRutl();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==retBtn){
WeBActivity.this.finish();
}

}


public void getRutl(){
web.setWebChromeClient(new WebChromeClient());    
web.setWebViewClient(new WebViewClient());    
web.getSettings().setJavaScriptEnabled(true);  
String s="https://hao.360.cn/?src=bm";//WebView访问的网址
web.loadUrl(s);
web.addJavascriptInterface(new Handler(), "handler");
web.setWebViewClient(new WebViewClient() {
@Override

public void onPageFinished(WebView view, String url) {

//ChinaPNRCmdId 获取值得参数

view.loadUrl("javascript:window.handler.show(document.getElementById('ChinaPNRCmdId').value);");
super.onPageFinished(view, url);
}
});




}

class Handler {

public void show(String data) {

//获取到的值


Toast.makeText(WeBActivity.this, data, 0).show();
}



}

你可能感兴趣的:(Android WebView中使用Java调用JavaScript方法并获取返回值)