FROM : http://blog.csdn.net/hj563308597/article/details/45197709
Android WebView
在公司Android的开发过程中遇到一个新的问题,那就是让android代码和Javascript代码进行交互,然后在网上找到了关于webView的资料,之前并没有深入去了解WebView,一直以为Android 的WebView是一个用来显示网页的组件而已,没想到功能如此强大,竟然能跟JS互调传参,太牛了!
1、什么是webView?
WebView是一个webkit的内核,android对其做了很好的优化跟封装,主要用来处理网页相关的问题,盗图一张
2、webView相关类的作用可以去查api,网站上面也很多,主要就是WebChromeClient,WebViewClient、WebSettings几个类,比较关键的就是加载本地js网页跟网络js网页的时候要有点区别,注意权限跟访问方式有点点区别,下面贴代码:
- package com.chinaonenet.mywebview;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.widget.Button;
- /**
- * @author hujun
- *
- * QQ:563008597
- *
- * SuppressLint一定要加上去!!!
- * 低版本可能没问题,高版本JS铁定调不了Android里面的方法
- *
- */
- @SuppressLint("SetJavaScriptEnabled")
- public class MainActivity extends Activity {
- //声明一个webview
- private WebView mWebView;
- private MyWebViewClient WVClient;
- private WebSettings webSettings;
- private MyWebChromeClient chromeClient;
- //测试Button,忽略就行
- private Button testBtn;
- //调用JS的Button
- private Button callJSBtn;
- //双击返回退出应用
- private long exitTime = 0;
- //Object对象,用来跟JS网页绑定
- private JSObject jsobject;
- //android调用JS网页的时候会用到
- private Handler mHandler = new Handler();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- private void initView() {
- // TODO Auto-generated method stub
- mWebView = (WebView) findViewById(R.id.webview);
- testBtn = (Button)findViewById(R.id.diyWeb);
- callJSBtn = (Button)findViewById(R.id.calljs);
- WVClient = new MyWebViewClient();
- chromeClient = new MyWebChromeClient();
- jsobject = new JSObject(MainActivity.this);
- webSettings = mWebView.getSettings();
- webSettings.setJavaScriptEnabled(true);
- webSettings.setBuiltInZoomControls(true);
- webSettings.setSavePassword(false);
- //支持多种分辨率,需要js网页支持
- webSettings.setUserAgentString("mac os");
- webSettings.setDefaultTextEncodingName("utf-8");
- //显示本地js网页
- mWebView.loadUrl(StringUrl.TEST_NET);
- mWebView.setWebViewClient(WVClient);
- mWebView.setWebChromeClient(chromeClient);
- //注意第二个参数JsTest,这个是JS网页调用Android方法的一个类似ID的东西
- mWebView.addJavascriptInterface(jsobject, "JsTest");
- //测试自定义网页,忽略
- testBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String str = "test my web";
- mWebView.loadData("<html><body>"+str+"<html><body>", "text/html", "UTF-8");
- }
- });
- //调用JS网页
- callJSBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- //调用JS中的 函数,当然也可以不传参
- mWebView.loadUrl("javascript:androidCallJS('顺便传个参数给JS!')");
- }
- });
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- //单击系统返回键网页回退
- @Override
- public void onBackPressed() {
- // TODO Auto-generated method stub
- //if (System.currentTimeMillis() - exitTime > 1500) {
- if (mWebView.canGoBack()) {
- mWebView.goBack();
- }/*else {
- Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
- }
- exitTime = System.currentTimeMillis();
- } */
- else {
- this.finish();
- }
- }
- }
- package com.chinaonenet.mywebview;
- import android.content.Context;
- import android.util.Log;
- import android.webkit.JavascriptInterface;
- import android.widget.Toast;
- public class JSObject {
- /*
- * 绑定的object对象
- * */
- private Context context;
- public JSObject(Context context){
- this.context = context;
- }
- /*
- * JS调用android的方法
- * @JavascriptInterface仍然必不可少
- *
- * */
- @JavascriptInterface
- public String JsCallAndroid(){
- Toast.makeText(context, "JsCallAndroid", Toast.LENGTH_SHORT).show();
- return "JS call Andorid";
- }
- }
下面上传工程下载链接:http://download.csdn.net/detail/hj563308597/8624449
http://www.cnblogs.com/greatverve/archive/2012/01/18/android-javascript.html