下面是webview常規的用法:javascript
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;html
public class MainActivity extends Activity {
//在Android 4.3系統及其一下WebView內部採用Webkit渲染引擎,在Android 4.4採用chromium 渲染引擎來渲染View的內容。
//別忘記,在androidMainFest.xml中添加網絡權限
//<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//<uses-permission android:name="android.permission.INTERNET" />
//<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
WebView test_wb;
JSUtil jsUtil;java
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//原生和webview交互的類
jsUtil = new JSUtil();
//xml佈局頁面中定義
test_wb=(WebView)findViewById(R.id.test_wb);
//清理
//wv.clearCache(true);
//wv.clearHistory();
//wv.clearFormData();
//設置webview屬性
WebSettings ws=test_wb.getSettings();
//通常都會設置支持js
ws.setJavaScriptEnabled(true);
//默認編碼格式
ws.setDefaultTextEncodingName("UTF-8");
//設置頁面自適應
ws.setUseWideViewPort(true);
//設置緩存模式
ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
//……………………
android
//獲取焦點
test_wb.requestFocus();
//注入webview對象
test_wb.addJavascriptInterface(jsUtil, "local_obj");//jsUtil對象類方法中添加了@JavascriptInterface標註,經過"window.local_obj.方法名"調用
//加載網頁
test_wb.loadUrl("http://www.zjtax.net");
//加載本地地址,file://android_asset/test.html
// test_wb.loadUrl("file:///android_asset/test.html");----------------這裏是file:/// 3個/線哦
//執行js,javascript:alert()
// test_wb.loadUrl("javascript:alert('ok')");
//主要處理解析,渲染網頁等瀏覽器作的事情
//若不重寫webviewclient的shouldOverrideUrlLoading方法,加載網頁會默認用外部的瀏覽器打開//
test_wb.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView wb,String url)
{
test_wb.loadUrl(url);
return true;//表示手動處理,若沒有loadUrl(url)頁面不會跳轉。
//return false;//若不想手動處理,直接用webview自動處理,能夠直接返回false.
//return super.shouldOverrideUrlLoading(wb, url);
}
//頁面開始請求
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}web
//頁面請求結束
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}瀏覽器
//頁面有錯誤
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
);
緩存
//是輔助WebView處理Javascript的對話框,網站圖標,網站title,加載進度等
//重寫處理網頁的alert,confirm,Prompt ,Window,Progress
test_wb.setWebChromeClient(new WebChromeClient() {
//進度條
@Override
public void onProgressChanged(WebView view, int newProgress) {
pb.setProgress(newProgress);
if(newProgress==100){
pb.setVisibility(View.GONE);
}
}
//網站標題
@Override
public void onReceivedTitle(WebView view, String title) {
// TODO Auto-generated method stub
super.onReceivedTitle(view, title);
}網絡
//網站圖標
@Override
public void onReceivedTouchIconUrl(WebView view, String url,
boolean precomposed) {
// TODO Auto-generated method stub
super.onReceivedTouchIconUrl(view, url, precomposed);
}app
//窗體
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
// TODO Auto-generated method stub
return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
}
@Override
public boolean onJsBeforeUnload(WebView view, String url,
String message, JsResult result) {
return super.onJsBeforeUnload(view, url, message, result);
}
/**
* 覆蓋默認的window.alert展現界面,避免title裏顯示爲「:來自file:////」
*/
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("對話框")
.setMessage(message)
.setPositiveButton("肯定", null);
// 不須要綁定按鍵事件
// 屏蔽keycode等於84之類的按鍵
builder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
Log.v("onJsAlert", "keyCode==" + keyCode + "event="+ event);
return true;
}
});
// 禁止響應按back鍵的事件
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
result.confirm();// 由於沒有綁定事件,須要強行confirm,不然頁面會變黑顯示不了內容。
return true;
// return super.onJsAlert(view, url, message, result);
}
/**
* 覆蓋默認的window.confirm展現界面,避免title裏顯示爲「:來自file:////」
*/
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("對話框")
.setMessage(message)
.setPositiveButton("肯定",new OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
result.confirm();
}
})
.setNeutralButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
result.cancel();
}
});
// 屏蔽keycode等於84之類的按鍵,避免按鍵後致使對話框消息而頁面沒法再彈出對話框的問題
builder.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
Log.v("onJsConfirm", "keyCode==" + keyCode + "event="+ event);
return true;
}
});
// 禁止響應按back鍵的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
// return super.onJsConfirm(view, url, message, result);
}
/**
* 覆蓋默認的window.prompt展現界面,避免title裏顯示爲「:來自file:////」
* window.prompt('請輸入您的域名地址', '618119.com');
*/
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, final JsPromptResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle("對話框").setMessage(message);
final EditText et = new EditText(view.getContext());
et.setSingleLine();
et.setText(defaultValue);
builder.setView(et)
.setPositiveButton("肯定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm(et.getText().toString());
}
})
.setNeutralButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
// 屏蔽keycode等於84之類的按鍵,避免按鍵後致使對話框消息而頁面沒法再彈出對話框的問題
builder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
Log.v("onJsPrompt", "keyCode==" + keyCode + "event="+ event);
return true;
}
});
// 禁止響應按back鍵的事件
// builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
// return super.onJsPrompt(view, url, message, defaultValue,
// result);
}
});
}
}ide