类型补充
This commit is contained in:
parent
e091197527
commit
31ffe13029
|
|
@ -34,6 +34,7 @@ public class FirstActivity extends BaseActivity {
|
|||
private static final String TAG = "FirstActivity";
|
||||
private WebView webView;
|
||||
protected WebViewLogger webViewLogger;
|
||||
private final Handler handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
// 扩展的 MIME 类型映射
|
||||
private static final Map<String, String> MIME_TYPES = new HashMap<>();
|
||||
|
|
@ -52,6 +53,27 @@ public class FirstActivity extends BaseActivity {
|
|||
MIME_TYPES.put("woff2", "font/woff2");
|
||||
MIME_TYPES.put("ttf", "font/ttf");
|
||||
MIME_TYPES.put("wasm", "application/wasm");
|
||||
|
||||
MIME_TYPES.put("pdf", "application/pdf");
|
||||
MIME_TYPES.put("zip", "application/zip");
|
||||
MIME_TYPES.put("rar", "application/x-rar-compressed");
|
||||
MIME_TYPES.put("7z", "application/x-7z-compressed");
|
||||
MIME_TYPES.put("csv", "text/csv");
|
||||
MIME_TYPES.put("txt", "text/plain");
|
||||
MIME_TYPES.put("mp3", "audio/mpeg");
|
||||
MIME_TYPES.put("mp4", "video/mp4");
|
||||
MIME_TYPES.put("mov", "video/quicktime");
|
||||
MIME_TYPES.put("webm", "video/webm");
|
||||
MIME_TYPES.put("ogg", "audio/ogg");
|
||||
MIME_TYPES.put("wav", "audio/wav");
|
||||
MIME_TYPES.put("eot", "application/vnd.ms-fontobject");
|
||||
MIME_TYPES.put("otf", "font/otf");
|
||||
MIME_TYPES.put("doc", "application/msword");
|
||||
MIME_TYPES.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
MIME_TYPES.put("xls", "application/vnd.ms-excel");
|
||||
MIME_TYPES.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
MIME_TYPES.put("ppt", "application/vnd.ms-powerpoint");
|
||||
MIME_TYPES.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -253,47 +275,7 @@ public class FirstActivity extends BaseActivity {
|
|||
if (dm != null) {
|
||||
currentDownloadId = dm.enqueue(request);
|
||||
Toast.makeText(this, "文件已开始下载,请等待", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// ✅ 注册广播监听下载完成事件
|
||||
registerReceiver(new android.content.BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, android.content.Intent intent) {
|
||||
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
||||
if (id == currentDownloadId) {
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
query.setFilterById(id);
|
||||
android.database.Cursor cursor = dm.query(query);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
|
||||
if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) {
|
||||
String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
|
||||
if (uriString != null) {
|
||||
try {
|
||||
Uri fileUri = Uri.parse(uriString);
|
||||
String type = getMimeTypeForUrl(fileUri.toString());
|
||||
|
||||
android.content.Intent openIntent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
|
||||
openIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK | android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
openIntent.setDataAndType(fileUri, type);
|
||||
|
||||
// ✅ 检查系统是否有能打开的 App
|
||||
if (openIntent.resolveActivity(context.getPackageManager()) != null) {
|
||||
context.startActivity(openIntent);
|
||||
} else {
|
||||
Toast.makeText(context, "下载完成,请前往“下载”文件夹手动打开该文件", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(context, "下载完成,但无法打开文件: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
// ✅ 只监听一次下载任务
|
||||
unregisterReceiver(this);
|
||||
}
|
||||
}
|
||||
}, new android.content.IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
startDownloadProgressMonitor(dm, currentDownloadId);
|
||||
} else {
|
||||
Toast.makeText(this, "系统下载服务不可用", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
|
@ -304,6 +286,49 @@ 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();
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}).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 ignored) {}
|
||||
}
|
||||
|
||||
public static class JsBridge {
|
||||
private final WeakReference<FirstActivity> activityRef;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue