下载文件 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); String file = "https://xxxx/20241115145431899326_20241115145431.jpg"; String fileName = file.substring(file.lastIndexOf("/") + 1); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file)); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); request.setTitle(fileName); request.setDescription(""); downloadManager.enqueue(request);
// 注册广播接收器,监听下载完成结果 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); registerReceiver(broadcastReceiver, intentFilter);
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { Log.d("dl", "DOWNLOAD_COMPLETE"); runOnUiThread(()-> { Toast.makeText(MainActivity.this, "DOWNLOAD_COMPLETE", Toast.LENGTH_LONG).show(); }); } } };