Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android download manager completed

Small question about the download manager in android. It's the first time I'm working with it and have successfully downloaded multiple files and opened them. But my question is how do i check if the download completed.

The situation is I download a PDF file and open it, and usually the file is so small it complets before opening. But if the file is somewhat bigger how do I check if the download manager is finished with the download before opening it.

How I download:

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); 

How I open the file

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); 

So somewhere between downloading and opening the file I want a if statement to check if it should continue or wait for the file.

like image 206
Msmit1993 Avatar asked Jan 31 '14 10:01

Msmit1993


People also ask

Where do download manager files go Android?

You can find your downloads on your Android device in your My Files app (called File Manager on some phones), which you can find in the device's App Drawer. Unlike iPhone, app downloads are not stored on the home screen of your Android device, and can be found with an upward swipe on the home screen.

How do I check download progress on Android?

Generally, the progress indicators in android are implemented by using the ProgressBar class. To display the progress indicators in our app, we need to add the progress bar to our notification by calling setProgress(max, progress, false) method and then issue the notification.

Do I need download manager on my Android?

If you're usually on an erratic mobile network, the chances of this happening are even higher. Hence, you need a download manager. Download managers can help you overcome several common hassles about downloading from the internet.

What is the use of download manager in Android?

The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file.


1 Answers

A broadcast is sent by the DownloadManager whenever a download completes, so you need to register a broadcast receiver with the appropriate intent action( ACTION_DOWNLOAD_COMPLETE ) to catch this broadcast:

To register receiver

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

and a BroadcastReciever handler

BroadcastReceiver onComplete=new BroadcastReceiver() {     public void onReceive(Context ctxt, Intent intent) {         // your code     } }; 

You can also create AsyncTask to handle the downloading of big files

Create a download dialog of some sort to display downloading in notification area and than handle the opening of the file:

protected void openFile(String fileName) {     Intent install = new Intent(Intent.ACTION_VIEW);     install.setDataAndType(Uri.fromFile(new File(fileName)),"MIME-TYPE");     startActivity(install); } 

you can also check the sample link

Sample Code

like image 190
Vaibhav Agarwal Avatar answered Nov 09 '22 14:11

Vaibhav Agarwal