国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android在WebView中調用系統下載的方法

2019-10-22 18:10:41
字體:
來源:轉載
供稿:網友

前言

最近發現項目中的WebView加載下載頁的時候是一片空白,沒有出現下載,于是簡單的調用了系統的下載對其進行下載。

過程

自定義一個下載監聽,實現了DownloadListener這個接口

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設置到WebView上;

mWebView.setWebViewDownListener(new MyDownloadStart());

設置Dialog,點擊是調用系統下載

DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downUrl));//下載時,下載完成后顯示通知request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//下載的路徑,第一個參數是文件夾名稱,第二個參數是下載的文件名request.setDestinationInExternalPublicDir("SooDown",dirName.getText().toString());request.setVisibleInDownloadsUi(true);downloadManager.enqueue(request);

這樣就可以進行下載了,但是我們是不知道什么時候下載完成的。通過DownloadManager下載完成系統會發送條廣播,我們要做的是要接收到該廣播并進行處理

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())){   //點擊通知欄進入下載管理頁面   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>

這樣基本就差不多可以了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开江县| 万全县| 榕江县| 武汉市| 吉首市| 泰宁县| 五大连池市| 鄯善县| 普安县| 永嘉县| 九江市| 镇安县| 广灵县| 洞头县| 沿河| 镇宁| 白银市| 塔城市| 云霄县| 昌宁县| 沙洋县| 丰台区| 临湘市| 郯城县| 南澳县| 大城县| 剑阁县| 本溪| 平顺县| 望江县| 巴马| 松阳县| 策勒县| 施秉县| 从化市| 舟山市| 犍为县| 伊吾县| 东城区| 夏河县| 兴城市|