有以下几个主要步骤:
1,首先要使你的webview能使用javascript。
WebView myWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true);2, 绑定JavaScript与Android代码。示例如下:
jsInterface = new JsInterface();
contentWebView.addJavascriptInterface(jsInterface, "android");
public class JsInterface {
/**
* 跳入另外一个webActivity.
* @param newUrl 跳转的地址
*/
@SuppressLint("JavascriptInterface")
public void goWebActivity(String newUrl) {
Intent intent = new Intent(BaseWebviewActivity.this,
BaseWebviewActivity.class);
intent.putExtra("url", Constants.WEBVIEW_URL + "/" + newUrl);
BaseWebviewActivity.this.startActivity(intent);
}
/**
* 关闭此activity.
*/
@SuppressLint("JavascriptInterface")
public void finishActivity() {
BaseWebviewActivity.this.finish();
}
@SuppressLint("JavascriptInterface")
public void goOtherActivity(int id) {
switch (id) {
case 0:
break;
}
}
/**
* 本地代码调用js代码.
*/
@SuppressLint("JavascriptInterface")
public void callJs() {
contentWebView.loadUrl("javascript:javacalljs()");
}
/**
* 本地代码调用js代码.
* @param data 数据
*/
@SuppressLint("JavascriptInterface")
public void callJs(String data) {
// 传递参数调用
contentWebView.loadUrl("javascript:javacalljswithargs(" + "'"
+ data + "'" + ")");
}
/**
* 页面出错后点击刷新按钮后的操作
*/
@SuppressLint("JavascriptInterface")
public void refesh() {
contentWebView.loadUrl(contentWebView.getNewUrl());
}
}
3,记得在webview的Activity上加上标注:
@SuppressLint("JavascriptInterface")
public class BaseWebviewActivity extends TaskBaseActivity {
private ProgressWebView contentWebView = null;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_web);
url = getIntent().getStringExtra("url");
if (url == null) {
url = "file:///android_asset/www/wrong.html";
}
contentWebView = (ProgressWebView) findViewById(R.id.webview);
setWebView();
}
}
4,关于ProgressWebView是额外写的。如下:
public class ProgressWebView extends WebView {
private String newUrl = null;
private final String ERRORURL = "file:///android_asset/www/wrong.html";
private ProgressDialog pd;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
setWebChromeClient(new WebChromeClient());
setWebViewClient(new WebClient());
pd = new ProgressDialog(context);
}
public class WebClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
view.stopLoading();
view.clearView();
view.loadUrl(ERRORURL);
dismissPd();
// DataUtil.clearDialog();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!url.equals(ERRORURL)){
newUrl = url;
}
view.loadUrl(url);
return true;
}
}
@Override
public void loadUrl(String url) {
super.loadUrl(url);
if(url != null && !url.equals(ERRORURL)){
newUrl = url;
pd.setCanceledOnTouchOutside(false);
pd.setCancelable(true);
pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dismissPd();
}
});
pd.show();
pd.setContentView(R.layout.progress_bar);
pd.getWindow().setGravity(Gravity.CENTER);
pd.getWindow().setBackgroundDrawableResource(R.color.transparent);
}
}
public String getNewUrl() {
return newUrl;
}
public void setNewUrl(String newUrl) {
this.newUrl = newUrl;
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress >= 100) {
dismissPd();
} else {
}
super.onProgressChanged(view, newProgress);
}
}
private void dismissPd(){
if(pd != null && pd.isShowing()) {
pd.dismiss();
}
}
}
5,如果网络问题,访问不到html页面,可以在本地做一个wrong.html,点击html某个直接返回。
url = getIntent().getStringExtra("url");
if (url == null) {
url = "file:///android_asset/www/wrong.html";
}