對於webview的簡單使用在這裏不作過多的說明,使用webview加載網頁的核心方法是java
public void loadUrl(String url) {}
下面就是介紹圍繞webview進行的一系列的配置。webview與js的交互在個人另外一篇博客:http://my.oschina.net/gef/blog/617576,本文中不涉及與js交互的總結。web
首先若是你調用了下面代碼:瀏覽器
webView = (WebView) findViewById(R.id.webview); webView.loadUrl("http://www.baidu.com");
你會發現你的app會自動打開手機系統自帶的默認瀏覽器,這時候咱們須要加一行:緩存
webView.setWebViewClient(new WebViewClient()}
而後再運行發現就不調用默認瀏覽器了,下面爲你們講講WebViewClient這個類:WebViewClient類中的幾個方法在咱們平時開發中大量的運用,首先是網絡
public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; }
通常狀況下咱們不須要重寫,這個函數有一個返回值,當爲false時意思是咱們不用管,當前的webview自已加載這個url,當返回爲ture時,就讓咱們本身操做。
app
public void onPageFinished(WebView view, String url) { }
當頁面加載完成時調用,但須要注意的是ide
/** * When onPageFinished() is called, the * rendering picture may not be updated yet. To get the notification for the * new Picture, use {@link WebView.PictureListener#onNewPicture}. */
也就是渲染圖片有可能沒有加載完成。函數
/** * Notify the host application that the WebView will load the resource * specified by the given url. * * @param view The WebView that is initiating the callback. * @param url The url of the resource the WebView will load. */ public void onLoadResource(WebView view, String url) { }
這個函數是這要加載資源就會被調用。而後說一下錯誤處理的函數:佈局
/** * Report an error to the host application. These errors are unrecoverable * (i.e. the main resource is unavailable). The errorCode parameter * corresponds to one of the ERROR_* constants. * @param view The WebView that is initiating the callback. * @param errorCode The error code corresponding to an ERROR_* value. * @param description A String describing the error. * @param failingUrl The url that failed to load. * @deprecated Use {@link #onReceivedError(WebView, WebResourceRequest, WebResourceError) * onReceivedError(WebView, WebResourceRequest, WebResourceError)} instead. */ @Deprecated public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { }
重寫該函數讓你的app更加人性化。ui
那這裏咱們又有一個需求:咱們要有一個進度條,那咱們怎麼知道時時的加載進度呢,就不是在WebViewClient類中了,而是在WebChromeClient中:
/** * Tell the host application the current progress of loading a page. * @param view The WebView that initiated the callback. * @param newProgress Current page loading progress, represented by * an integer between 0 and 100. */ public void onProgressChanged(WebView view, int newProgress) {}
其中參數newProgress就是加載的時時進度。WebChromeClient中還用一些經常使用的函數,好比:
/** * Notify the host application of a change in the document title. * @param view The WebView that initiated the callback. * @param title A String containing the new title of the document. */ public void onReceivedTitle(WebView view, String title) {} /** * Notify the host application of a new favicon for the current page. * @param view The WebView that initiated the callback. * @param icon A Bitmap containing the favicon for the current page. */ public void onReceivedIcon(WebView view, Bitmap icon) {}
一個是能夠獲取網頁的title,一個是能夠title旁邊的icon。
而後說一下webview緩存問題:有時候咱們有緩存的需求,就是在沒有網絡的狀況下,之前能夠打開的網頁也能夠經過緩存文件打開,主要代碼爲:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webSettings.setAppCacheEnabled(true); String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/gefdemoweb"; Log.e(null,path); webSettings.setAppCachePath(path);
第一行設置了緩存模式,第二行設置能夠緩存,而後下面設置緩存路徑,關於緩存模式有不少種:
public static final int LOAD_DEFAULT = -1;//默認模式,當緩存資源是可用的不過時,就使用,否次網絡加載 public static final int LOAD_NORMAL = 0;//This value is obsolete,過期了,不用管 public static final int LOAD_CACHE_ELSE_NETWORK = 1;//當緩存資源是可用的就使用,即便它是過時的,否次網絡加載 public static final int LOAD_NO_CACHE = 2;//不使用緩存 public static final int LOAD_CACHE_ONLY = 3;//不使用網絡
而後說一下按返回鍵的問題,若是你不作任何設置,按返回鍵確定要跳到上一個activity,可是咱們想讓他返回到上一個加載的網頁怎麼辦:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){ webView.goBack(); return true; } return super.onKeyDown(keyCode, event); }
完美解決。
最後看一下webSetting的其它經常使用設置:
setJavaScriptEnabled(true); //支持js setPluginsEnabled(true); //支持插件 setUseWideViewPort(false); //將圖片調整到適合webview的大小 setSupportZoom(true); //支持縮放 setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); //支持內容從新佈局 supportMultipleWindows(); //多窗口 setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //關閉webview中緩存 setAllowFileAccess(true); //設置能夠訪問文件 setNeedInitialFocus(true); //當webview調用requestFocus時爲webview設置節點 webview webSettings.setBuiltInZoomControls(true); //設置支持縮放 setJavaScriptCanOpenWindowsAutomatically(true); //支持經過JS打開新窗口 setLoadWithOverviewMode(true); // 縮放至屏幕的大小 setLoadsImagesAutomatically(true); //支持自動加載圖片
關於webview與js的交互,請看個人另外一篇博客:http://my.oschina.net/gef/blog/617576
歡迎補充和糾正