文件下载恢复到单线程
This commit is contained in:
parent
569c025169
commit
f5d1e9b98d
|
|
@ -246,24 +246,18 @@ public class FirstActivity extends BaseActivity {
|
||||||
// || url.contains("/api/file/");
|
// || url.contains("/api/file/");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private long currentDownloadId = -1; // 保存当前下载任务ID
|
private long currentDownloadId = -1; // 保存当前下载任务ID
|
||||||
/**
|
|
||||||
* ✅ 下载文件并调用系统 DownloadManager
|
|
||||||
*/
|
|
||||||
private void downloadFile(String url) {
|
private void downloadFile(String url) {
|
||||||
new Thread(() -> {
|
|
||||||
try {
|
try {
|
||||||
if (TextUtils.isEmpty(url)) {
|
if (TextUtils.isEmpty(url)) {
|
||||||
handler.post(() ->
|
Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(this, "下载地址为空", Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.startsWith("blob:") || url.startsWith("data:")) {
|
if (url.startsWith("blob:") || url.startsWith("data:")) {
|
||||||
handler.post(() ->
|
Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -289,31 +283,24 @@ public class FirstActivity extends BaseActivity {
|
||||||
|
|
||||||
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
||||||
if (dm != null) {
|
if (dm != null) {
|
||||||
handler.post(() ->
|
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
currentDownloadId = dm.enqueue(request);
|
currentDownloadId = dm.enqueue(request);
|
||||||
startDownloadProgressMonitor(dm, currentDownloadId);
|
startDownloadProgressMonitor(dm, currentDownloadId);
|
||||||
} else {
|
} else {
|
||||||
handler.post(() ->
|
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
handler.post(() ->
|
Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(this, "下载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ✅ 监控下载进度,每秒回调一次到前端
|
* ✅ 监控下载进度,每秒回调一次到前端
|
||||||
*/
|
*/
|
||||||
private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) {
|
private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) {
|
||||||
new Thread(() -> {
|
|
||||||
DownloadManager.Query query = new DownloadManager.Query();
|
DownloadManager.Query query = new DownloadManager.Query();
|
||||||
query.setFilterById(downloadId);
|
query.setFilterById(downloadId);
|
||||||
boolean downloading = true;
|
boolean downloading = true;
|
||||||
|
|
@ -326,56 +313,35 @@ public class FirstActivity extends BaseActivity {
|
||||||
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
||||||
long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
||||||
long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
||||||
|
|
||||||
if (status == DownloadManager.STATUS_RUNNING && total > 0) {
|
if (status == DownloadManager.STATUS_RUNNING && total > 0) {
|
||||||
int progress = (int) ((downloaded * 100L) / total);
|
int progress = (int) ((downloaded * 100L) / total);
|
||||||
handler.post(() -> {
|
Toast.makeText(this, "下载中,当前进度为" + progress + "%", Toast.LENGTH_SHORT).show();
|
||||||
sendProgressToJs(progress);
|
handler.post(() -> sendProgressToJs(progress));
|
||||||
// 可选:减少 toast 频率,比如每 20% 提示一次
|
|
||||||
if (progress % 20 == 0) {
|
|
||||||
Toast.makeText(this, "下载中:" + progress + "%", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||||
downloading = false;
|
|
||||||
handler.post(() -> {
|
|
||||||
sendProgressToJs(100);
|
|
||||||
Toast.makeText(this, "下载完成,请到文件管理查看", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "下载完成,请到文件管理查看", Toast.LENGTH_SHORT).show();
|
||||||
});
|
|
||||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
|
||||||
downloading = false;
|
downloading = false;
|
||||||
handler.post(() -> {
|
handler.post(() -> sendProgressToJs(100));
|
||||||
sendProgressToJs(-1);
|
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||||
Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "下载失败" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
|
||||||
});
|
downloading = false;
|
||||||
|
handler.post(() -> sendProgressToJs(-1));
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
e.printStackTrace();
|
Toast.makeText(this, "文件已开始下载,请等待报错", Toast.LENGTH_SHORT).show();
|
||||||
downloading = false;
|
|
||||||
handler.post(() ->
|
|
||||||
Toast.makeText(this, "监控异常:" + e.getMessage(), Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* ✅ 向前端回调进度
|
|
||||||
*/
|
|
||||||
private void sendProgressToJs(int progress) {
|
private void sendProgressToJs(int progress) {
|
||||||
try {
|
try {
|
||||||
if (webView != null) {
|
if (webView != null) {
|
||||||
String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress);
|
String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress);
|
||||||
webView.evaluateJavascript(js, null);
|
webView.evaluateJavascript(js, null);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
e.printStackTrace();
|
Toast.makeText(this, "文件已开始下载,请等待报错sendProgressToJs", Toast.LENGTH_SHORT).show();
|
||||||
handler.post(() ->
|
|
||||||
Toast.makeText(this, "进度回调异常:" + e.getMessage(), Toast.LENGTH_SHORT).show()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue