I'm using Android download manager class. I need to write "Download Complete" after all downloads are completed. I have tried something and it's working. But in here it's writing after file by file completed. I need to write only once (After all are completed). I also tried without a cursor. But I failed to achieve.
public class DownloadReceiver extends BroadcastReceiver {
Context context;
@Override
public void onReceive(Context context, Intent intent) {
long receivedID = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
DownloadManager mgr = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(receivedID);
Cursor cur = mgr.query(query);
int index = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
if(cur.) {
String filePath = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_TITLE));
if(cur.getInt(index) == DownloadManager.STATUS_SUCCESSFUL){
DatabaseHandler dh = new DatabaseHandler(context);
dh.addDownloadComplete(new DownloadComplete("Download Complete"));
}
}
cur.close();
}}
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...
}