文件下载提交
This commit is contained in:
parent
31ffe13029
commit
14f462050c
|
|
@ -38,6 +38,7 @@ public class FirstActivity extends BaseActivity {
|
||||||
|
|
||||||
// 扩展的 MIME 类型映射
|
// 扩展的 MIME 类型映射
|
||||||
private static final Map<String, String> MIME_TYPES = new HashMap<>();
|
private static final Map<String, String> MIME_TYPES = new HashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
MIME_TYPES.put("js", "text/javascript");
|
MIME_TYPES.put("js", "text/javascript");
|
||||||
MIME_TYPES.put("mjs", "text/javascript");
|
MIME_TYPES.put("mjs", "text/javascript");
|
||||||
|
|
@ -237,6 +238,7 @@ public class FirstActivity extends BaseActivity {
|
||||||
// || url.endsWith(".xlsx")
|
// || url.endsWith(".xlsx")
|
||||||
// || url.contains("/api/file/");
|
// || url.contains("/api/file/");
|
||||||
}
|
}
|
||||||
|
|
||||||
private long currentDownloadId = -1; // 保存当前下载任务ID
|
private long currentDownloadId = -1; // 保存当前下载任务ID
|
||||||
|
|
||||||
private void downloadFile(String url) {
|
private void downloadFile(String url) {
|
||||||
|
|
@ -274,7 +276,7 @@ public class FirstActivity extends BaseActivity {
|
||||||
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
|
||||||
if (dm != null) {
|
if (dm != null) {
|
||||||
currentDownloadId = dm.enqueue(request);
|
currentDownloadId = dm.enqueue(request);
|
||||||
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "文件已开始下载,请等待" + currentDownloadId, Toast.LENGTH_SHORT).show();
|
||||||
startDownloadProgressMonitor(dm, currentDownloadId);
|
startDownloadProgressMonitor(dm, currentDownloadId);
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
|
||||||
|
|
@ -290,34 +292,40 @@ public class FirstActivity extends BaseActivity {
|
||||||
* ✅ 监控下载进度,每秒回调一次到前端
|
* ✅ 监控下载进度,每秒回调一次到前端
|
||||||
*/
|
*/
|
||||||
private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) {
|
private void startDownloadProgressMonitor(DownloadManager dm, long downloadId) {
|
||||||
new Thread(() -> {
|
|
||||||
DownloadManager.Query query = new DownloadManager.Query();
|
|
||||||
query.setFilterById(downloadId);
|
|
||||||
boolean downloading = true;
|
|
||||||
|
|
||||||
while (downloading) {
|
DownloadManager.Query query = new DownloadManager.Query();
|
||||||
try {
|
query.setFilterById(downloadId);
|
||||||
Thread.sleep(1000);
|
boolean downloading = true;
|
||||||
android.database.Cursor cursor = dm.query(query);
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
while (downloading) {
|
||||||
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
try {
|
||||||
long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
Thread.sleep(1000);
|
||||||
long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
android.database.Cursor cursor = dm.query(query);
|
||||||
if (status == DownloadManager.STATUS_RUNNING && total > 0) {
|
Toast.makeText(this, "文件已开始下载,请等待" + cursor, Toast.LENGTH_SHORT).show();
|
||||||
int progress = (int) ((downloaded * 100L) / total);
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
handler.post(() -> sendProgressToJs(progress));
|
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
||||||
} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
long total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
||||||
downloading = false;
|
long downloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
||||||
handler.post(() -> sendProgressToJs(100));
|
Toast.makeText(this, "文件已开始下载,请等待" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
|
||||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
if (status == DownloadManager.STATUS_RUNNING && total > 0) {
|
||||||
downloading = false;
|
int progress = (int) ((downloaded * 100L) / total);
|
||||||
handler.post(() -> sendProgressToJs(-1));
|
Toast.makeText(this, "1.文件已开始下载,部分进度,调用前端js" + status + "," + total + "," + downloaded, Toast.LENGTH_SHORT).show();
|
||||||
}
|
handler.post(() -> sendProgressToJs(progress));
|
||||||
cursor.close();
|
} 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) {
|
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);
|
String js = String.format("if(window.onDownloadProgress){window.onDownloadProgress(%d);}", progress);
|
||||||
webView.evaluateJavascript(js, null);
|
webView.evaluateJavascript(js, null);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (Exception ignored) {
|
||||||
|
Toast.makeText(this, "文件已开始下载,请等待报错sendProgressToJs", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class JsBridge {
|
public static class JsBridge {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue