From 14f462050cb9afef2ce10363232255b8ea89e4e6 Mon Sep 17 00:00:00 2001 From: frank wang <425294756@qq.com> Date: Wed, 15 Oct 2025 12:07:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/pxkj/jwzs/FirstActivity.java | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/pxkj/jwzs/FirstActivity.java b/app/src/main/java/com/pxkj/jwzs/FirstActivity.java index 3a318e2..8fc540c 100644 --- a/app/src/main/java/com/pxkj/jwzs/FirstActivity.java +++ b/app/src/main/java/com/pxkj/jwzs/FirstActivity.java @@ -38,6 +38,7 @@ public class FirstActivity extends BaseActivity { // 扩展的 MIME 类型映射 private static final Map MIME_TYPES = new HashMap<>(); + static { MIME_TYPES.put("js", "text/javascript"); MIME_TYPES.put("mjs", "text/javascript"); @@ -237,6 +238,7 @@ public class FirstActivity extends BaseActivity { // || url.endsWith(".xlsx") // || url.contains("/api/file/"); } + private long currentDownloadId = -1; // 保存当前下载任务ID private void downloadFile(String url) { @@ -274,7 +276,7 @@ public class FirstActivity extends BaseActivity { DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); if (dm != null) { currentDownloadId = dm.enqueue(request); - Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show(); + Toast.makeText(this, "文件已开始下载,请等待" + currentDownloadId, Toast.LENGTH_SHORT).show(); startDownloadProgressMonitor(dm, currentDownloadId); } else { Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show(); @@ -290,34 +292,40 @@ public class FirstActivity extends BaseActivity { * ✅ 监控下载进度,每秒回调一次到前端 */ 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)); - if (status == DownloadManager.STATUS_RUNNING && total > 0) { - int progress = (int) ((downloaded * 100L) / total); - handler.post(() -> sendProgressToJs(progress)); - } else if (status == DownloadManager.STATUS_SUCCESSFUL) { - downloading = false; - handler.post(() -> sendProgressToJs(100)); - } else if (status == DownloadManager.STATUS_FAILED) { - downloading = false; - handler.post(() -> sendProgressToJs(-1)); - } - cursor.close(); + 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); + Toast.makeText(this, "文件已开始下载,请等待" + cursor, Toast.LENGTH_SHORT).show(); + 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)); + Toast.makeText(this, "文件已开始下载,请等待" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show(); + if (status == DownloadManager.STATUS_RUNNING && total > 0) { + int progress = (int) ((downloaded * 100L) / total); + Toast.makeText(this, "1.文件已开始下载,部分进度,调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show(); + handler.post(() -> sendProgressToJs(progress)); + } else if (status == DownloadManager.STATUS_SUCCESSFUL) { + Toast.makeText(this, "2.文件已开始下载,进度成功,调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show(); + downloading = false; + handler.post(() -> sendProgressToJs(100)); + } else if (status == DownloadManager.STATUS_FAILED) { + Toast.makeText(this, "3.文件已开始下载,进度失败,调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show(); + downloading = false; + handler.post(() -> sendProgressToJs(-1)); } - } catch (Exception ignored) {} + cursor.close(); + } + } catch (Exception ignored) { + Toast.makeText(this, "文件已开始下载,请等待报错", Toast.LENGTH_SHORT).show(); } - }).start(); + } } private void sendProgressToJs(int progress) { @@ -326,7 +334,9 @@ public class FirstActivity extends BaseActivity { String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress); webView.evaluateJavascript(js, null); } - } catch (Exception ignored) {} + } catch (Exception ignored) { + Toast.makeText(this, "文件已开始下载,请等待报错sendProgressToJs", Toast.LENGTH_SHORT).show(); + } } public static class JsBridge {