Android中的下载管理器有一个小问题。这是我第一次使用它,已经成功地下载了多个文件并打开了它们。但我的问题是如何检查下载是否完成。
情况是我下载一个PDF文件并打开它,通常文件很小,所以在打开之前就完成了。但如果文件稍微大一些,我该如何在打开它之前检查下载管理器是否已经完成了下载。
我的下载方式:
Intent intent = getIntent();
DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(intent.getStringExtra("Document_href"));
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications.
request.setTitle(intent.getStringExtra("Document_title"));
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,intent.getStringExtra("Document_title") + ".pdf");
//Enqueue a new download and same the referenceId
Long downloadReference = downloadManager.enqueue(request);
我如何打开这个文件
Uri uri = Uri.parse("content://com.app.applicationname/" + "/Download/" + intent.getStringExtra("Document_title") + ".pdf");
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(uri, "application/pdf");
target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(target);
所以在下载和打开文件的过程中,我希望能够添加一个if语句来检查它是否应该继续执行或等待文件。
registerReceiver
函数应该在哪个对象上调用? - sudoExclaimationExclaimationtry { unregisterReceiver(onComplete); } catch (Exception e) { e.printStackTrace(); } }
- UdayaLakmal