From f5d1e9b98d30757d4dc6103e39f5c54e57490aa2 Mon Sep 17 00:00:00 2001 From: frank wang <425294756@qq.com> Date: Fri, 31 Oct 2025 10:10:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E5=88=B0=E5=8D=95=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/pxkj/jwzs/FirstActivity.java | 182 +++++++----------- 1 file changed, 74 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/com/pxkj/jwzs/FirstActivity.java b/app/src/main/java/com/pxkj/jwzs/FirstActivity.java index c101e6d..4462051 100644 --- a/app/src/main/java/com/pxkj/jwzs/FirstActivity.java +++ b/app/src/main/java/com/pxkj/jwzs/FirstActivity.java @@ -246,136 +246,102 @@ public class FirstActivity extends BaseActivity { // || url.contains("/api/file/"); } + private long currentDownloadId = -1; // 保存当前下载任务ID - /** - * ✅ 下载文件并调用系统 DownloadManager - */ + private void downloadFile(String url) { - new Thread(() -> { - try { - if (TextUtils.isEmpty(url)) { - handler.post(() -> - Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show() - ); - return; - } - - if (url.startsWith("blob:") || url.startsWith("data:")) { - handler.post(() -> - Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show() - ); - return; - } - - Uri uri = Uri.parse(url); - DownloadManager.Request request = new DownloadManager.Request(uri); - - String fileName = URLUtil.guessFileName(url, null, null); - request.setTitle(fileName); - request.setDescription("正在下载文件..."); - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); - request.allowScanningByMediaScanner(); - request.addRequestHeader("User-Agent", System.getProperty("http.agent")); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - request.setRequiresCharging(false); - request.setAllowedOverMetered(true); - request.setAllowedOverRoaming(true); - } - - String mimeType = getMimeTypeForUrl(url); - request.setMimeType(!TextUtils.isEmpty(mimeType) ? mimeType : "application/octet-stream"); - - DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); - if (dm != null) { - handler.post(() -> - Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show() - ); - currentDownloadId = dm.enqueue(request); - startDownloadProgressMonitor(dm, currentDownloadId); - } else { - handler.post(() -> - Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show() - ); - } - - } catch (Exception e) { - e.printStackTrace(); - handler.post(() -> - Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show() - ); + try { + if (TextUtils.isEmpty(url)) { + Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show(); + return; } - }).start(); + + if (url.startsWith("blob:") || url.startsWith("data:")) { + Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show(); + return; + } + + Uri uri = Uri.parse(url); + DownloadManager.Request request = new DownloadManager.Request(uri); + + String fileName = URLUtil.guessFileName(url, null, null); + request.setTitle(fileName); + request.setDescription("正在下载文件..."); + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); + request.allowScanningByMediaScanner(); + request.addRequestHeader("User-Agent", System.getProperty("http.agent")); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + request.setRequiresCharging(false); + request.setAllowedOverMetered(true); + request.setAllowedOverRoaming(true); + } + + String mimeType = getMimeTypeForUrl(url); + request.setMimeType(!TextUtils.isEmpty(mimeType) ? mimeType : "application/octet-stream"); + + DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); + if (dm != null) { + Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show(); + currentDownloadId = dm.enqueue(request); + startDownloadProgressMonitor(dm, currentDownloadId); + } else { + Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show(); + } + + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show(); + } } /** * ✅ 监控下载进度,每秒回调一次到前端 */ private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) { - new Thread(() -> { - DownloadManager.Query query = new DownloadManager.Query(); - query.setFilterById(downloadId); - boolean downloading = true; - while (downloading) { - try { - Thread.sleep(1000); - android.database.Cursor cursor = dm.query(query); - if (cursor != null && cursor.moveToFirst()) { - int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); - long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); - long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); + DownloadManager.Query query = new DownloadManager.Query(); + query.setFilterById(downloadId); + boolean downloading = true; - if (status == DownloadManager.STATUS_RUNNING && total > 0) { - int progress = (int) ((downloaded * 100L) / total); - handler.post(() -> { - sendProgressToJs(progress); - // 可选:减少 toast 频率,比如每 20% 提示一次 - if (progress % 20 == 0) { - Toast.makeText(this, "下载中:" + progress + "%", Toast.LENGTH_SHORT).show(); - } - }); - } else if (status == DownloadManager.STATUS_SUCCESSFUL) { - downloading = false; - handler.post(() -> { - sendProgressToJs(100); - Toast.makeText(this, "下载完成,请到文件管理查看", Toast.LENGTH_SHORT).show(); - }); - } else if (status == DownloadManager.STATUS_FAILED) { - downloading = false; - handler.post(() -> { - sendProgressToJs(-1); - Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show(); - }); - } - cursor.close(); + while (downloading) { + try { + Thread.sleep(1000); + android.database.Cursor cursor = dm.query(query); + if (cursor != null && cursor.moveToFirst()) { + int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); + long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); + long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); + if (status == DownloadManager.STATUS_RUNNING && total > 0) { + int progress = (int) ((downloaded * 100L) / total); + Toast.makeText(this, "下载中,当前进度为" + progress + "%", Toast.LENGTH_SHORT).show(); + handler.post(() -> sendProgressToJs(progress)); + } else if (status == DownloadManager.STATUS_SUCCESSFUL) { + Toast.makeText(this, "下载完成,请到文件管理查看", Toast.LENGTH_SHORT).show(); + downloading = false; + handler.post(() -> sendProgressToJs(100)); + } else if (status == DownloadManager.STATUS_FAILED) { + Toast.makeText(this, "下载失败" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show(); + downloading = false; + handler.post(() -> sendProgressToJs(-1)); } - } catch (Exception e) { - e.printStackTrace(); - downloading = false; - handler.post(() -> - Toast.makeText(this, "监控异常:" + e.getMessage(), Toast.LENGTH_SHORT).show() - ); + cursor.close(); } + } catch (Exception ignored) { + Toast.makeText(this, "文件已开始下载,请等待报错", Toast.LENGTH_SHORT).show(); } - }).start(); + } } - /** - * ✅ 向前端回调进度 - */ private void sendProgressToJs(int progress) { try { if (webView != null) { String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress); webView.evaluateJavascript(js, null); } - } catch (Exception e) { - e.printStackTrace(); - handler.post(() -> - Toast.makeText(this, "进度回调异常:" + e.getMessage(), Toast.LENGTH_SHORT).show() - ); + } catch (Exception ignored) { + Toast.makeText(this, "文件已开始下载,请等待报错sendProgressToJs", Toast.LENGTH_SHORT).show(); } }