Android--WebView開發項目使用這些就夠了
WebView基本使用:
1、加載網頁、刷新網頁、前進、后退
在xml文件中
<WebView
android:id="@+id/wv_test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在java代碼中:
WebView wvTest = (WebView)findViewById(R.id.wv_banner);
//加載網絡網頁
wvTest.loadUrl("http://www.baidu.com");
//加載本地網頁
wvTest.loadData("file:///android_asset/refresh/refresh.html");
//返回上個頁面
if(wvTest.canGoBack()){
wvTest.goBack();
}
//去剛才瀏覽的頁面
if(wvTest.canGoForward()){
wvTest.goForward();
}
//刷新當前頁面
wvTest.reload();
2、設置一些參數
注: 需要用到什么設置,隨時查一下即可。最后一條在加載含有Html5新標簽的網頁的時候需要加上這句話。
WebSettings settings = wvTest.getSettings();
settings.setJavaScriptEnabled(true);//支持javascript
settings.setUseWideViewPort(true);//適配屏幕
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);//支持放大縮小
settings.setDisplayZoomControls(false);//隱藏放大縮小的按鈕
settings.setDomStorageEnabled(true);//支持Html5標簽
……等
3、獲取網頁標題
class MyWebViewChromeClient extends WebChromeClient{
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
L.D("title就是網頁標題:" + title);
}
}
wvTest.setWebChromeClient(new MyWebViewChromeClient());
4、下載文件
wvTest.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
//需要下載的文件的地址就是url,及可以下載文件
}
});
調用系統瀏覽器去下載文件(updateUrl是下載地址):
Intent intent= new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(updateUrl);
intent.setData(content_url);
startActivity(Intent.createChooser(intent, "請選擇瀏覽器"));
5、WebView錯誤碼處理
就在網頁加載失敗、沒有網絡的情況下WebView會把加載的網頁地址顯示出來。為了避免這種情況,我們需要做如下處理:
class MyWebViewClient extends WebViewClient{
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//在這里進行加載錯誤、沒有網絡情況的處理
//方式1:加載一個本地的默認頁面
wvTest.loadData("file:///android_asset/refresh/refresh.html");
//方式2:顯示一個默認布局
tvDefault.setText("網頁加載失敗");
wvTest.setVisibility(View.GONE);
}
}
wvTest.setWebViewClient(new MyWebViewClient());
6、和JavaScript交互(簡單的調用、傳值)
Java代碼:
//被javascript調用的類
class CalledByJs {
private Context mContext;
public CalledByJs(Context context){
this.mContext = context;
}
//在js中被調用的方法
@JavascriptInterface
public void callFromJs(){
Toast.makeText(mContext, "js調用啦~", Toast.LENGTH_LONG).show();
}
}
settings.setJavaScriptEnabled(true);
wvTest.addJavascriptInterface(new CalledByJs(this), "demo");
JavaScript代碼:
function call(){
demo.callFromJs();
}
<img src="img/refreshicon.png" onclick="call()" alt="輕觸屏幕重新刷新" />
7、WebView自定義攔截
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!TextUtils.isEmpty(url)){
if("open_login".equals(url)){
Intent intent = new Intent(this,LoginActivity.class);
startActivity(intent);
return true;
}else {
return false;
}
}
return super.shouldOverrideUrlLoading(view, url);
}
這里解釋一下這個方法的返回值:
返回true:
webview處理url是根據程序來執行的。即:如果返回為true,即不再會通過webview加載,而是執行你寫的攔截的程序,如果沒有的話就不執行(不執行就是,內容有href,超鏈接也不會跳轉了)
返回false:
webview處理url是在webview內部執行。返回false的話就可以執行內容的超鏈接跳轉。
8、清空WebView緩存
/**
* WebView清空緩存
*/
public static void clearCache(Context context) {
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
context.deleteDatabase("webviewCookiesChromium.db");
context.deleteDatabase("webviewCookiesChromiumPrivate.db");
//WebView 緩存文件
File webviewCacheDir = new File(context.getCacheDir().getAbsolutePath()+"/webviewCacheChromium");
//刪除webview 緩存目錄
if(webviewCacheDir.exists()){
deleteFile(webviewCacheDir);
}
}
來自:http://www.jianshu.com/p/7223e35da485
本文由用戶 binbin60 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!