Android在WebView中調(diào)用系統(tǒng)下載的方法
前言
最近發(fā)現(xiàn)項(xiàng)目中的WebView加載下載頁(yè)的時(shí)候是一片空白,沒(méi)有出現(xiàn)下載,于是簡(jiǎn)單的調(diào)用了系統(tǒng)的下載對(duì)其進(jìn)行下載。
過(guò)程
自定義一個(gè)下載監(jiān)聽(tīng),實(shí)現(xiàn)了DownloadListener這個(gè)接口
class MyDownloadStart implements DownloadListener{
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
downUrl = url;
//從鏈接里獲取文件名
String dirNameString = url.substring(url.lastIndexOf("/") + 1);
//獲得下載文件的大小
DecimalFormat decimalFormat = new DecimalFormat("0.00");
float size = contentLength;
dirName.setText(dirNameString);
if (size < 1024){
dirSize.setText(size + "B");
}else if (size < 1048576){
String dirSizeStringKB = decimalFormat.format(size / 1024);
dirSize.setText(dirSizeStringKB + "K");
}else if (size < 1073741824){
String dirSizeString = decimalFormat.format(size / 1048576);
dirSize.setText(dirSizeString + "M");
}else {
String dirStringG = decimalFormat.format(size / 1073741824);
dirSize.setText(dirStringG + "G");
}
//顯示是否下載的dialog
downdialog.show();
}
}
將MyDownloadStart設(shè)置到WebView上;
mWebView.setWebViewDownListener(new MyDownloadStart());
設(shè)置Dialog,點(diǎn)擊是調(diào)用系統(tǒng)下載
DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downUrl));
//下載時(shí),下載完成后顯示通知
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//下載的路徑,第一個(gè)參數(shù)是文件夾名稱,第二個(gè)參數(shù)是下載的文件名
request.setDestinationInExternalPublicDir("SooDown",dirName.getText().toString());
request.setVisibleInDownloadsUi(true);
downloadManager.enqueue(request);
這樣就可以進(jìn)行下載了,但是我們是不知道什么時(shí)候下載完成的。通過(guò)DownloadManager下載完成系統(tǒng)會(huì)發(fā)送條廣播,我們要做的是要接收到該廣播并進(jìn)行處理
public class DownloadReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())){
Toast.makeText(context,"下載完成",Toast.LENGTH_SHORT).show();
}else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())){
//點(diǎn)擊通知欄進(jìn)入下載管理頁(yè)面
Intent intent1 = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
最后一步,不要忘記配置BroadcastReceiver
在AndroidManifest.xml中配置
<receiver android:name=".Utils.DownloadReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
</intent-filter>
</receiver>
這樣基本就差不多可以了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
TableLayout(表格布局)基礎(chǔ)知識(shí)點(diǎn)詳解
在本文里我們給大家分享了關(guān)于TableLayout(表格布局)的相關(guān)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02
Android實(shí)現(xiàn)頂部導(dǎo)航菜單左右滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頂部導(dǎo)航菜單左右滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
詳解Android開(kāi)發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限)
這篇文章主要介紹了詳解Android開(kāi)發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android ScrollView無(wú)法填充滿屏幕的解決辦法
這篇文章主要介紹了Android ScrollView無(wú)法填充滿屏幕的解決辦法的相關(guān)資料,這里提供實(shí)例和解決辦法,需要的朋友可以參考下2017-07-07
android 通過(guò)向viewpage中添加listview來(lái)完成滑動(dòng)效果(類似于qq滑動(dòng)界面)
android 通過(guò)向viewpage中添加listview來(lái)完成滑動(dòng)效果(類似于qq滑動(dòng)界面),需要的朋友可以參考一下2013-05-05

