网上的例子稍微改动了一下, 调了半天才在android 4.2上跑过。
html文件:
<html>
<script type="text/javascript">
function wave() {
alert("wave!!!!");
document.getElementById("droid").src="android_waving.png";
}
function clickhandler() {
document.getElementById("log").innerHTML="log clickhandler before handled";
js2java.clickOnAndroid();
document.getElementById("log").innerHTML="log clickhandler after handled";
}
</script>
<body>
<!--<a οnclick="js2java.clickOnAndroid()">-->
<!--<a href="javascript:window.js2java.clickOnAndroid()">-->
<a οnclick="clickhandler()">
<!--<a οnclick="wave()">-->
<img id="droid" src="android_normal.png"/>
</a>
<p id="log">log origin</p>
</body>
</html>
java code:
package com.sony.photo.mediatest;
//import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.View;
import android.view.View.OnKeyListener;
import android.content.Context;
//import android.view.SurfaceHolder;
//import android.view.SurfaceView;
//import com.sony.btv.photoplayer.PhotoPlayerJni;
//import com.sony.btv.photoplayer.MpTypes;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.JavascriptInterface;
/**
*
*/
public class MediaTestActivity extends Activity {
private static final String TAG = "BRUCE MEDIADEMO";
WebView webView;
private Handler mHandler = new Handler();
//final String mimeType = "text/html";
//final String encoding = "utf-8";
class JSObj {
private Context context;
public JSObj(Context con) {
this.context = con;
}
@JavascriptInterface
public void clickOnAndroid() {
Log.i(TAG, "clickOnAndroid");
mHandler.post(new Runnable() {
public void run() {
webView.loadUrl("javascript:wave()");
}
});
//webView.loadUrl("javascript:wave()");
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.clearCache(true);
//webView.setOnKeyListener(new keyListener());
webView.getSettings().setJavaScriptEnabled(true);
//webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
JSObj jsobj = new JSObj(this);
webView.addJavascriptInterface(jsobj, "js2java");
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.i(TAG, "overrideurlloading");
return true;
}
});
loadLocalHtml();
}
/*
class keyListener implements OnKeyListener {
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i(TAG, "keyListener");
return onKeyDown(keyCode, event);
}
}
//@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
Log.i(TAG, "bruce onkeydown KeyEvent.KEYCODE_BACK, webview can go back");
webView.goBack();
return true;
}
else if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.i(TAG, "bruce onkeydown KeyEvent.KEYCODE_BACK");
onDestroy();
return true;
}
else if (keyCode == KeyEvent.KEYCODE_ENTER) {
Log.i(TAG, "bruce onkeydown KeyEvent.KEYCODE_ENTER");
webView.loadUrl("javascript:wave()");
return true;
}
return super.onKeyDown(keyCode, event);
}
*/
/** load local html */
private void loadLocalHtml() {
try {
Log.i(TAG, "loadLocalHtml");
webView.loadUrl("file:///android_asset/demo.html");
//webView.loadUrl("javascript:wave()");
Log.i(TAG, "after loadLocalHtml");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
finish();
}
}
webview的clearcache能清除以前错误执行的东东。
自己实现的obj class需要加上@JavascriptInterface, 否则java代码不能被调用。
这个解释了这个@注解的作用:http://zhidao.baidu.com/question/350040333.html
xml布局文件就不贴出来了。 就是linear layout里面有个webview. 不用也行的。