I'm learning how to use DownloadManager
with BroadcastReceiver
to know when the download is complete.
The problem: let's suppose I'm going to download 2 PDFs on one page, I register 2 Receivers, that is, one for each PDF, then I put each of them in the download queue (see the dm.enqueue
call) . What happens is that for every completed download (status == 8) I get 2 times this notification.
In the application when the page has only 2 PDFs to download, dm.enqueue(request);
is called only twice as expected.
I thought: should the completed download notification be sent only once for each file?
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription(getString(R.string.download_description));
request.setTitle(getString(R.string.download_title));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request
.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment
.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
dm = (DownloadManager) getSystemService(Context
.DOWNLOAD_SERVICE);
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Log.d(TAG, "onReceive called");
// http://stackoverflow.com/a/13322285/3697611
Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager
.EXTRA_DOWNLOAD_ID));
Cursor c = dm.query(q);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex
(DownloadManager.COLUMN_STATUS));
// se estiver completo
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String uri = c.getString(c.getColumnIndex
(DownloadManager.COLUMN_URI));
String local_uri = c.getString(c.getColumnIndex
(DownloadManager.COLUMN_LOCAL_URI));
//TODO: bug, estas mensagens estão aparecendo 2
// vezes para cada download
display(getString(R.string.completed) + uri);
display(getString(R.string.saved) + local_uri);
try {
// quando completar o download devemos
// pegar o texto
getText(new File(new URI(local_uri)));
} catch (URISyntaxException e) {
printStackTrace(e);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager
.ACTION_DOWNLOAD_COMPLETE));
receivers.add(receiver);
dm.enqueue(request);
Q.: This is just a simple application that I did to develop my knowledge about Android, currently it searches for PDFs on the page where they publish the official diary of the city where I live.
Note: the app has been tested on a Alcatel Pixi 4'6 with Android 5.1.
Debugging
When I put a logd I see the messages like this:
09-04 15:10:28.395 30024-30252/com.example.android.officialdiary D/com.example.android.officialdiary.MainActivity: download: dm.enqueue register a request
09-04 15:10:28.422 30024-30252/com.example.android.officialdiary D/com.example.android.officialdiary.MainActivity: download: dm.enqueue register a request
09-04 15:10:33.288 30024-30024/com.example.android.officialdiary D/com.example.android.officialdiary.MainActivity: download: onReceive called
09-04 15:10:33.312 30024-30024/com.example.android.officialdiary D/com.example.android.officialdiary.MainActivity: download: onReceive called
09-04 15:10:34.593 30024-30024/com.example.android.officialdiary D/com.example.android.officialdiary.MainActivity: download: onReceive called
09-04 15:10:34.609 30024-30024/com.example.android.officialdiary D/com.example.android.officialdiary.MainActivity: download: onReceive called
We noticed that two requests
were registered (at this moment there are 2 PDFs in the portal), but for each of them onReceive was fired twice (total gave 4/2 = 2).
Note: If you want to test the code faster just start the debugging section of Android Studio and when the App starts uncheck "Extract text" and check "Clear data" (it will delete the downloaded PDFs and download again but will not extract the text from the PDFs).