Android Download Manager Completed

Android download manager completed

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

How to notify after all download completed from Android download manager

Add a check before notifying all downloads are completed. like

query.setFilterByStatus(DownloadManager.STATUS_PAUSED |
DownloadManager.STATUS_PENDING |
DownloadManager.STATUS_RUNNING);
Cursor cursor = downloadManager.query(query);
if (cursor != null && cursor.getCount() > 0) {
return;
}else {
query.setFilterById(receivedID);
//proceed your...
}

How can I wait until DownloadManager is complete?

Use a BroadcastReceiver to detect when the download finishes:

public class DownloadBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
//Show a notification
}
}
}

and register it in your manifest:

<receiver android:name=".DownloadBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>

Finding download complete from downloadManager in android

I found my answer at : DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android

and the code for me goes like this:

private boolean downloadComplete(long downloadId){
DownloadManager dMgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Cursor c= dMgr.query(new DownloadManager.Query().setFilterById(downloadId));

if(c.moveToFirst()){
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));

if(status == DownloadManager.STATUS_SUCCESSFUL){
return true; //Download completed, celebrate
}else{
int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
Log.d(TAG, "Download not correct, status [" + status + "] reason [" + reason + "]");
return false;
}
}
return false;
}


Related Topics



Leave a reply



Submit