文件下载恢复到单线程
This commit is contained in:
parent
569c025169
commit
f5d1e9b98d
|
|
@ -246,24 +246,18 @@ 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()
|
||||
);
|
||||
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()
|
||||
);
|
||||
Toast.makeText(this, "无法直接下载 Blob 文件,请检查前端下载逻辑", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -289,31 +283,24 @@ public class FirstActivity extends BaseActivity {
|
|||
|
||||
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
||||
if (dm != null) {
|
||||
handler.post(() ->
|
||||
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show()
|
||||
);
|
||||
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
|
||||
currentDownloadId = dm.enqueue(request);
|
||||
startDownloadProgressMonitor(dm, currentDownloadId);
|
||||
} else {
|
||||
handler.post(() ->
|
||||
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show()
|
||||
);
|
||||
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
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) {
|
||||
new Thread(() -> {
|
||||
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
query.setFilterById(downloadId);
|
||||
boolean downloading = true;
|
||||
|
|
@ -326,56 +313,35 @@ public class FirstActivity extends BaseActivity {
|
|||
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);
|
||||
// 可选:减少 toast 频率,比如每 20% 提示一次
|
||||
if (progress % 20 == 0) {
|
||||
Toast.makeText(this, "下载中:" + progress + "%", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
Toast.makeText(this, "下载中,当前进度为" + progress + "%", Toast.LENGTH_SHORT).show();
|
||||
handler.post(() -> sendProgressToJs(progress));
|
||||
} 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();
|
||||
});
|
||||
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));
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
downloading = false;
|
||||
handler.post(() ->
|
||||
Toast.makeText(this, "监控异常:" + e.getMessage(), Toast.LENGTH_SHORT).show()
|
||||
);
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue