一. 基本用法
1. Webview
- 基本
// 獲取當(dāng)前頁面的URL
public String getUrl();
// 獲取當(dāng)前頁面的原始URL(重定向后可能當(dāng)前url不同)
// 就是http headers的Referer參數(shù),loadUrl時為null
public String getOriginalUrl();
// 獲取當(dāng)前頁面的標(biāo)題
public String getTitle();
// 獲取當(dāng)前頁面的favicon
public Bitmap getFavicon();
// 獲取當(dāng)前頁面的加載進度
public int getProgress();
// 通知WebView內(nèi)核網(wǎng)絡(luò)狀態(tài)
// 用于設(shè)置JS屬性`window.navigator.isOnline`和產(chǎn)生HTML5事件`online/offline`
public void setNetworkAvailable(boolean networkUp)
// 設(shè)置初始縮放比例
public void setInitialScale(int scaleInPercent);
- 加載網(wǎng)頁
// 加載URL指定的網(wǎng)頁
public void loadUrl(String url);
// 攜帶http headers加載URL指定的網(wǎng)頁
public void loadUrl(String url, Map<String, String> additionalHttpHeaders);
// 使用POST請求加載指定的網(wǎng)頁
public void postUrl(String url, byte[] postData);
// 重新加載當(dāng)前網(wǎng)頁
public void reload();
// 加載內(nèi)容
public void loadData(String data, String mimeType, String encoding);
// 使用baseUrl加載內(nèi)容
public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl);
- JavaScript
// 注入Javascript對象
public void addJavascriptInterface(Object object, String name);
// 移除已注入的Javascript對象,下次加載或刷新頁面時生效
public void removeJavascriptInterface(String name);
// 對傳入的JS表達式求值,通過resultCallback返回結(jié)果
// 此函數(shù)添加于API19,必須在UI線程中調(diào)用,回調(diào)也將在UI線程
public void evaluateJavascript(String script, ValueCallback<String> resultCallback)
- 導(dǎo)航(前進后退)
// 復(fù)制一份BackForwardList
public WebBackForwardList copyBackForwardList();
// 是否可后退
public boolean canGoBack();
// 是否可前進
public boolean canGoForward();
// 是否可前進/后退steps頁,大于0表示前進小于0表示后退
public boolean canGoBackOrForward(int steps);
// 后退一頁
public void goBack();
// 前進一頁
public void goForward();
// 前進/后退steps頁,大于0表示前進小于0表示后退
public void goBackOrForward(int steps);
// 清除當(dāng)前webview訪問的歷史記錄
public void clearHistory();
- 網(wǎng)頁查找
// 設(shè)置網(wǎng)頁查找結(jié)果回調(diào)
public void setFindListener(FindListener listener);
// 異步執(zhí)行查找網(wǎng)頁內(nèi)包含的字符串并設(shè)置高亮,查找結(jié)果會回調(diào).
public void findAllAsync (String find);
// 查找下一個匹配的字符串
public void findNext (boolean forward);
// 清除網(wǎng)頁查找的高亮匹配字符串
public void clearMatches();
- 截屏/翻頁/縮放
// 保存網(wǎng)頁(.html)到指定文件
public void saveWebArchive(String filename);
// 保存網(wǎng)頁(.html)到文件
public void saveWebArchive(String basename, boolean autoname, ValueCallback<String> callback);
// 上翻一頁,即向上滾動WebView高度的一半
public void pageUp(boolean top);
// 下翻一頁,即向下滾動WebView高度的一半
public void pageDown(boolean bottom);
// 縮放
public void zoomBy(float factor);
// 放大
public boolean zoomIn();
// 縮放
public boolean zoomOut();
- 其它
// 清除網(wǎng)頁緩存,由于內(nèi)核緩存是全局的因此這個方法不僅僅針對webview而是針對整個應(yīng)用程序
public void clearCache(boolean includeDiskFiles);
// 清除自動完成填充的表單數(shù)據(jù)
public void clearFormData();
// 清除SSL偏好
public void clearSslPreferences();
// 查詢文檔中是否有圖片,查詢結(jié)果將被發(fā)送到msg.getTarget()
// 如果包含圖片,msg.arg1 為1,否則為0
public void documentHasImages(Message msg);
// 請求最近輕叩(tapped)的 錨點/圖像 元素的URL,查詢結(jié)果將被發(fā)送到msg.getTarget()
// msg.getData()中的url是錨點的href屬性,title是錨點的文本,src是圖像的src
public void requestFocusNodeHref(Message msg);
// 請求最近觸摸(touched)的 圖像元素的URL,查詢結(jié)果將被發(fā)送到msg.getTarget()
// msg.getData()中的url是圖像鏈接
public void requestImageRef(Message msg)
// 清除證書請求偏好,添加于API21
// 在WebView收到`android.security.STORAGE_CHANGED` Intent時會自動清除
public static void clearClientCertPreferences(Runnable onCleared)
// 開啟網(wǎng)頁內(nèi)容(js,css,html...)調(diào)試模式,添加于API19
public static void setWebContentsDebuggingEnabled(boolean enabled)
2. webSettings
WebSettings settings = web.getSettings();
// 存儲(storage)
// 啟用HTML5 DOM storage API,默認(rèn)值 false
settings.setDomStorageEnabled(true);
// 啟用Web SQL Database API,這個設(shè)置會影響同一進程內(nèi)的所有WebView,默認(rèn)值 false
// 此API已不推薦使用,參考:https://www.w3.org/TR/webdatabase/
settings.setDatabaseEnabled(true);
// 啟用Application Caches API,必需設(shè)置有效的緩存路徑才能生效,默認(rèn)值 false
// 此API已廢棄,參考:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Using_the_application_cache
settings.setAppCacheEnabled(true);
settings.setAppCachePath(context.getCacheDir().getAbsolutePath());
// 定位(location)
settings.setGeolocationEnabled(true);
// 是否保存表單數(shù)據(jù)
settings.setSaveFormData(true);
// 是否當(dāng)webview調(diào)用requestFocus時為頁面的某個元素設(shè)置焦點,默認(rèn)值 true
settings.setNeedInitialFocus(true);
// 是否支持viewport屬性,默認(rèn)值 false
// 頁面通過`<meta name="viewport" ... />`自適應(yīng)手機屏幕
settings.setUseWideViewPort(true);
// 是否使用overview mode加載頁面,默認(rèn)值 false
// 當(dāng)頁面寬度大于WebView寬度時,縮小使頁面寬度等于WebView寬度
settings.setLoadWithOverviewMode(true);
// 布局算法
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
// 是否支持Javascript,默認(rèn)值false
settings.setJavaScriptEnabled(true);
// 是否支持多窗口,默認(rèn)值false
settings.setSupportMultipleWindows(false);
// 是否可用Javascript(window.open)打開窗口,默認(rèn)值 false
settings.setJavaScriptCanOpenWindowsAutomatically(false);
// 資源訪問
settings.setAllowContentAccess(true); // 是否可訪問Content Provider的資源,默認(rèn)值 true
settings.setAllowFileAccess(true); // 是否可訪問本地文件,默認(rèn)值 true
// 是否允許通過file url加載的Javascript讀取本地文件,默認(rèn)值 false
settings.setAllowFileAccessFromFileURLs(false);
// 是否允許通過file url加載的Javascript讀取全部資源(包括文件,http,https),默認(rèn)值 false
settings.setAllowUniversalAccessFromFileURLs(false);
// 資源加載
settings.setLoadsImagesAutomatically(true); // 是否自動加載圖片
settings.setBlockNetworkImage(false); // 禁止加載網(wǎng)絡(luò)圖片
settings.setBlockNetworkLoads(false); // 禁止加載所有網(wǎng)絡(luò)資源
// 縮放(zoom)
settings.setSupportZoom(true); // 是否支持縮放
settings.setBuiltInZoomControls(false); // 是否使用內(nèi)置縮放機制
settings.setDisplayZoomControls(true); // 是否顯示內(nèi)置縮放控件
// 默認(rèn)文本編碼,默認(rèn)值 "UTF-8"
settings.setDefaultTextEncodingName("UTF-8");
settings.setDefaultFontSize(16); // 默認(rèn)文字尺寸,默認(rèn)值16,取值范圍1-72
settings.setDefaultFixedFontSize(16); // 默認(rèn)等寬字體尺寸,默認(rèn)值16
settings.setMinimumFontSize(8); // 最小文字尺寸,默認(rèn)值 8
settings.setMinimumLogicalFontSize(8); // 最小文字邏輯尺寸,默認(rèn)值 8
settings.setTextZoom(100); // 文字縮放百分比,默認(rèn)值 100
// 字體
settings.setStandardFontFamily("sans-serif"); // 標(biāo)準(zhǔn)字體,默認(rèn)值 "sans-serif"
settings.setSerifFontFamily("serif"); // 襯線字體,默認(rèn)值 "serif"
settings.setSansSerifFontFamily("sans-serif"); // 無襯線字體,默認(rèn)值 "sans-serif"
settings.setFixedFontFamily("monospace"); // 等寬字體,默認(rèn)值 "monospace"
settings.setCursiveFontFamily("cursive"); // 手寫體(草書),默認(rèn)值 "cursive"
settings.setFantasyFontFamily("fantasy"); // 幻想體,默認(rèn)值 "fantasy"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 用戶是否需要通過手勢播放媒體(不會自動播放),默認(rèn)值 true
settings.setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 5.0以上允許加載http和https混合的頁面(5.0以下默認(rèn)允許,5.0+默認(rèn)禁止)
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 是否在離開屏幕時光柵化(會增加內(nèi)存消耗),默認(rèn)值 false
settings.setOffscreenPreRaster(false);
}
if (isNetworkConnected(context)) {
// 根據(jù)cache-control決定是否從網(wǎng)絡(luò)上取數(shù)據(jù)
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
// 沒網(wǎng),離線加載,優(yōu)先加載緩存(即使已經(jīng)過期)
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
// deprecated
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setDatabasePath(context.getDir("database", Context.MODE_PRIVATE).getPath());
settings.setGeolocationDatabasePath(context.getFilesDir().getPath());
大部分保持默認(rèn)值就行
WebSettings settings = web.getSettings();
// 緩存(cache)
settings.setAppCacheEnabled(true); // 默認(rèn)值 false
settings.setAppCachePath(context.getCacheDir().getAbsolutePath());
// 存儲(storage)
settings.setDomStorageEnabled(true); // 默認(rèn)值 false
settings.setDatabaseEnabled(true); // 默認(rèn)值 false
// 是否支持viewport屬性,默認(rèn)值 false
// 頁面通過`<meta name="viewport" ... />`自適應(yīng)手機屏幕
settings.setUseWideViewPort(true);
// 是否使用overview mode加載頁面,默認(rèn)值 false
// 當(dāng)頁面寬度大于WebView寬度時,縮小使頁面寬度等于WebView寬度
settings.setLoadWithOverviewMode(true);
// 是否支持Javascript,默認(rèn)值false
settings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 5.0以上允許加載http和https混合的頁面(5.0以下默認(rèn)允許,5.0+默認(rèn)禁止)
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
if (isNetworkConnected(context)) {
// 根據(jù)cache-control決定是否從網(wǎng)絡(luò)上取數(shù)據(jù)
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
// 沒網(wǎng),離線加載,優(yōu)先加載緩存(即使已經(jīng)過期)
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
3. WebClient
// 攔截頁面加載,返回true表示宿主app攔截并處理了該url,否則返回false由當(dāng)前WebView處理
// 此方法在API24被廢棄,不處理POST請求
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
// 攔截頁面加載,返回true表示宿主app攔截并處理了該url,否則返回false由當(dāng)前WebView處理
// 此方法添加于API24,不處理POST請求,可攔截處理子frame的非http請求
@TargetApi(Build.VERSION_CODES.N)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrlLoading(view, request.getUrl().toString());
}
// 此方法廢棄于API21,調(diào)用于非UI線程
// 攔截資源請求并返回響應(yīng)數(shù)據(jù),返回null時WebView將繼續(xù)加載資源
// 注意:API21以下的AJAX請求會走onLoadResource,無法通過此方法攔截
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return null;
}
// 此方法添加于API21,調(diào)用于非UI線程
// 攔截資源請求并返回數(shù)據(jù),返回null時WebView將繼續(xù)加載資源
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return shouldInterceptRequest(view, request.getUrl().toString());
}
// 頁面(url)開始加載
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
// 頁面(url)完成加載
public void onPageFinished(WebView view, String url) {
}
// 將要加載資源(url)
public void onLoadResource(WebView view, String url) {
}
// 這個回調(diào)添加于API23,僅用于主框架的導(dǎo)航
// 通知應(yīng)用導(dǎo)航到之前頁面時,其遺留的WebView內(nèi)容將不再被繪制。
// 這個回調(diào)可以用來決定哪些WebView可見內(nèi)容能被安全地回收,以確保不顯示陳舊的內(nèi)容
// 它最早被調(diào)用,以此保證WebView.onDraw不會繪制任何之前頁面的內(nèi)容,隨后繪制背景色或需要加載的新內(nèi)容。
// 當(dāng)HTTP響應(yīng)body已經(jīng)開始加載并體現(xiàn)在DOM上將在隨后的繪制中可見時,這個方法會被調(diào)用。
// 這個回調(diào)發(fā)生在文檔加載的早期,因此它的資源(css,和圖像)可能不可用。
// 如果需要更細粒度的視圖更新,查看 postVisualStateCallback(long, WebView.VisualStateCallback).
// 請注意這上邊的所有條件也支持 postVisualStateCallback(long ,WebView.VisualStateCallback)
public void onPageCommitVisible(WebView view, String url) {
}
// 此方法廢棄于API23
// 主框架加載資源時出錯
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
// 此方法添加于API23
// 加載資源時出錯,通常意味著連接不到服務(wù)器
// 由于所有資源加載錯誤都會調(diào)用此方法,所以此方法應(yīng)盡量邏輯簡單
@TargetApi(Build.VERSION_CODES.M)
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (request.isForMainFrame()) {
onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
}
}
// 此方法添加于API23
// 在加載資源(iframe,image,js,css,ajax...)時收到了 HTTP 錯誤(狀態(tài)碼>=400)
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
// 是否重新提交表單,默認(rèn)不重發(fā)
public void onFormResubmission(WebView view, Message dontResend, Message resend) {
dontResend.sendToTarget();
}
// 通知應(yīng)用可以將當(dāng)前的url存儲在數(shù)據(jù)庫中,意味著當(dāng)前的訪問url已經(jīng)生效并被記錄在內(nèi)核當(dāng)中。
// 此方法在網(wǎng)頁加載過程中只會被調(diào)用一次,網(wǎng)頁前進后退并不會回調(diào)這個函數(shù)。
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
}
// 加載資源時發(fā)生了一個SSL錯誤,應(yīng)用必需響應(yīng)(繼續(xù)請求或取消請求)
// 處理決策可能被緩存用于后續(xù)的請求,默認(rèn)行為是取消請求
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.cancel();
}
// 此方法添加于API21,在UI線程被調(diào)用
// 處理SSL客戶端證書請求,必要的話可顯示一個UI來提供KEY。
// 有三種響應(yīng)方式:proceed()/cancel()/ignore(),默認(rèn)行為是取消請求
// 如果調(diào)用proceed()或cancel(),Webview 將在內(nèi)存中保存響應(yīng)結(jié)果且對相同的"host:port"不會再次調(diào)用 onReceivedClientCertRequest
// 多數(shù)情況下,可通過KeyChain.choosePrivateKeyAlias啟動一個Activity供用戶選擇合適的私鑰
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
request.cancel();
}
// 處理HTTP認(rèn)證請求,默認(rèn)行為是取消請求
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.cancel();
}
// 通知應(yīng)用有個已授權(quán)賬號自動登陸了
public void onReceivedLoginRequest(WebView view, String realm, String account, String args) {
}
// 給應(yīng)用一個機會處理按鍵事件
// 如果返回true,WebView不處理該事件,否則WebView會一直處理,默認(rèn)返回false
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
return false;
}
// 處理未被WebView消費的按鍵事件
// WebView總是消費按鍵事件,除非是系統(tǒng)按鍵或shouldOverrideKeyEvent返回true
// 此方法在按鍵事件分派時被異步調(diào)用
public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
super.onUnhandledKeyEvent(view, event);
}
// 通知應(yīng)用頁面縮放系數(shù)變化
public void onScaleChanged(WebView view, float oldScale, float newScale) {
}
4. WebChromeClient
// 獲得所有訪問歷史項目的列表,用于鏈接著色。
public void getVisitedHistory(ValueCallback<String[]> callback) {
}
// <video /> 控件在未播放時,會展示為一張海報圖,HTML中可通過它的'poster'屬性來指定。
// 如果未指定'poster'屬性,則通過此方法提供一個默認(rèn)的海報圖。
public Bitmap getDefaultVideoPoster() {
return null;
}
// 當(dāng)全屏的視頻正在緩沖時,此方法返回一個占位視圖(比如旋轉(zhuǎn)的菊花)。
public View getVideoLoadingProgressView() {
return null;
}
// 接收當(dāng)前頁面的加載進度
public void onProgressChanged(WebView view, int newProgress) {
}
// 接收文檔標(biāo)題
public void onReceivedTitle(WebView view, String title) {
}
// 接收圖標(biāo)(favicon)
public void onReceivedIcon(WebView view, Bitmap icon) {
}
// Android中處理Touch Icon的方案
// http://droidyue.com/blog/2015/01/18/deal-with-touch-icon-in-android/index.html
public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
}
// 通知應(yīng)用當(dāng)前頁進入了全屏模式,此時應(yīng)用必須顯示一個包含網(wǎng)頁內(nèi)容的自定義View
public void onShowCustomView(View view, CustomViewCallback callback) {
}
// 通知應(yīng)用當(dāng)前頁退出了全屏模式,此時應(yīng)用必須隱藏之前顯示的自定義View
public void onHideCustomView() {
}
// 顯示一個alert對話框
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return false;
}
// 顯示一個confirm對話框
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
return false;
}
// 顯示一個prompt對話框
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
return false;
}
// 顯示一個對話框讓用戶選擇是否離開當(dāng)前頁面
public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
return false;
}
// 指定源的網(wǎng)頁內(nèi)容在沒有設(shè)置權(quán)限狀態(tài)下嘗試使用地理位置API。
// 從API24開始,此方法只為安全的源(https)調(diào)用,非安全的源會被自動拒絕
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
}
// 當(dāng)前一個調(diào)用 onGeolocationPermissionsShowPrompt() 取消時,隱藏相關(guān)的UI。
public void onGeolocationPermissionsHidePrompt() {
}
// 通知應(yīng)用打開新窗口
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
return false;
}
// 通知應(yīng)用關(guān)閉窗口
public void onCloseWindow(WebView window) {
}
// 請求獲取取焦點
public void onRequestFocus(WebView view) {
}
// 通知應(yīng)用網(wǎng)頁內(nèi)容申請訪問指定資源的權(quán)限(該權(quán)限未被授權(quán)或拒絕)
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onPermissionRequest(PermissionRequest request) {
request.deny();
}
// 通知應(yīng)用權(quán)限的申請被取消,隱藏相關(guān)的UI。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onPermissionRequestCanceled(PermissionRequest request) {
}
// 為'<input type="file" />'顯示文件選擇器,返回false使用默認(rèn)處理
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
return false;
}
// 接收J(rèn)avaScript控制臺消息
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return false;
}
在獲取webView實例后,進行相關(guān)設(shè)置,包括:WebChromeClient(輔助WebView處理Javascript的對話框、網(wǎng)站圖標(biāo)、網(wǎng)站title、加載進度等),WebViewClient(輔助WebView處理各種通知與請求事件),WebSettings(WebView相關(guān)配置的設(shè)置)
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true); //設(shè)置WebView屬性,運行執(zhí)行js腳本
settings.setUseWideViewPort(true);//將圖片調(diào)整到適合webView的大小
settings.setLoadWithOverviewMode(true); //自適應(yīng)屏幕
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setSupportZoom(true);//設(shè)定支持縮放
settings.setDefaultTextEncodingName("utf-8");
settings.setLoadsImagesAutomatically(true);
webView.loadUrl("http://www.baidu.com/");//調(diào)用loadUrl方法為WebView加入鏈接,測試環(huán)境
webView.setWebViewClient(new WebViewClient() {
//設(shè)置在webView點擊打開的新網(wǎng)頁在當(dāng)前界面顯示,而不跳轉(zhuǎn)到新的瀏覽器中
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().getPath());
return true;
}
});
刷新頁面:webView.reload();
參考文獻: 必知必會 | WebView 的一切都在這兒
二. 下載文件
1. 思路:
給webView設(shè)置下載監(jiān)聽器setDownloadListener,在監(jiān)聽方法onDownloadStart方法中實現(xiàn)下載邏輯,下載的方法有:
- 跳轉(zhuǎn)到系統(tǒng)瀏覽器下載
- 使用系統(tǒng)的下載服務(wù)
- 自定義下載任務(wù)
2. 代碼:
- 系統(tǒng)瀏覽器下載:簡單省事,但不能進行后續(xù)處理,無法知道下載過程
private void downloadByBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(url));
startActivity(intent);
}
- 系統(tǒng)服務(wù)下載:這種方式操作簡單,兼容性好、支持?jǐn)嚯娎m(xù)傳、大文件下載、通知欄進度提示等等
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//指定下載路徑和下載文件名
request.setDestinationInExternalPublicDir("/download/", URLUtil.guessFileName(url, contentDisposition, mimetype));
//獲取下載管理器
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
//將下載任務(wù)加入下載隊列,否則不會進行下載
downloadManager.enqueue(request);
Toast.makeText(this,"下載完成:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath() + File.separator + URLUtil.guessFileName(url, contentDisposition, mimetype), Toast.LENGTH_SHORT).show();
- 自定義線程下載:代碼量大,但是高效靈活
@Override
public void run() {
InputStream in = null;
FileOutputStream fout = null;
try {
URL httpUrl = new URL(dlUrl);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
in = conn.getInputStream();
File downloadFile, sdFile;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
downloadFile = Environment.getExternalStorageDirectory();
sdFile = new File(downloadFile, "xx");
fout = new FileOutputStream(sdFile);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
fout.write(buffer, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三. 上傳文件
1. 思路:
重寫WebChromeClient,在WebViewActivity中接收選擇到的文件Uri,傳給頁面通過js完成上傳:
public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
private void openImageChooserActivity() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i,
"Image Chooser"), FILE_CHOOSER_RESULT_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (uploadMessage != null) {
uploadMessage.onReceiveValue(result);
uploadMessage = null;
}
}
}
2. 注意點:
- 由于系統(tǒng)版本的不同,文件選擇器打開的方式可能不一樣
- 即使獲取的結(jié)果為null,也要傳給web,即直接調(diào)用mUploadMessage.onReceiveValue(null),否則網(wǎng)頁會阻塞
- 打release包時,WebChromeClient里的openFileChooser方法不要混淆
3. 代碼:
/**
* @date: 7/12/17
* @description: 5.0以下的回調(diào)
*/
private ValueCallback<Uri> uploadMessage;
/**
* @date: 7/12/17
* @description: 5.0以上的回調(diào)
*/
private ValueCallback<Uri[]> uploadMessageAboveL;
/**
* @date: 7/12/17
* @description: 文件選擇碼
*/
private final static int FILE_CHOOSER_RESULT_CODE = 1;
//webView上傳文件,不同版本的適配,注意:里面的方法不能混淆
webView.setWebChromeClient(new WebChromeClient() {
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> valueCallback) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
// For Android >= 3.0
public void openFileChooser(ValueCallback valueCallback, String acceptType) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
//For Android >= 4.1
public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
uploadMessage = valueCallback;
openImageChooserActivity();
}
// For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
openImageChooserActivity();
return true;
}
});
private void openImageChooserActivity() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (null == uploadMessage && null == uploadMessageAboveL) return;
Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
if (uploadMessageAboveL != null) {
onActivityResultAboveL(requestCode, resultCode, data);
} else if (uploadMessage != null) {
uploadMessage.onReceiveValue(result);
uploadMessage = null;
}
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)
return;
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (intent != null) {
String dataString = intent.getDataString();
ClipData clipData = intent.getClipData();
if (clipData != null) {
results = new Uri[clipData.getItemCount()];
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
results[i] = item.getUri();
}
}
if (dataString != null)
results = new Uri[]{Uri.parse(dataString)};
}
}
uploadMessageAboveL.onReceiveValue(results);
uploadMessageAboveL = null;
}
4. 實用工具:
選擇文件會使用系統(tǒng)提供的組件或者其他支持的app,返回的uri有的直接是文件的url,有的是contentprovider的uri,因此我們需要統(tǒng)一處理一下,轉(zhuǎn)成文件的uri,可參考以下代碼(獲取文件的路徑),調(diào)用getPath可以將Uri轉(zhuǎn)成真實文件的Path,然后可以自己生成文件的Uri
public class FileUtils {
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
}