(1) js调用android的方法: WebView wView; wView. addJavascriptInterface (Object obj, String interfaceName); 是实例化一个对象,在html的js中调用,第二个参数是实例化对象的别名,如果要使用这个obj,则在js中使用的名字 就是interfaceName。 public class jsWebDemo extends Activity { WebView wView;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wView = (WebView)findViewById(R.id.wv1);
这里的AliansBridge即是调用的JAVA对象。 要注意的是,如果你只有一个方法需要被js调用只new一个对象就行: ProxyBridge pBridge = new ProxyBridge(); wView.addJavascriptInterface(pBridge, "AliansBridge"); 如果你需要调用好多个方法,就要把整个android程序所在的类本身实例化传递进去: wView.addJavascriptInterface(this, "AliansBridge"); (2) android调用js的方法:wView.setWebChromeClient (WebChromeClient client) 要用到Handler更新UI,Handler mHandler = new Handler(); mWebView.setWebChromeClient(new MyWebChromeClient()); mWebView.addJavascriptInterface(new Object(){ /** * This is not called on the UI thread. Post a runnable to invoke * loadUrl on the UI thread. */ public void clickOnAndroid() {