文件下载提交
This commit is contained in:
parent
31ffe13029
commit
14f462050c
|
|
@ -38,6 +38,7 @@ public class FirstActivity extends BaseActivity {
|
|||
|
||||
// 扩展的 MIME 类型映射
|
||||
private static final Map<String, String> 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,7 +292,7 @@ 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;
|
||||
|
|
@ -299,25 +301,31 @@ public class FirstActivity extends BaseActivity {
|
|||
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));
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
} 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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue