Question

I'm working on an android application.

I'm using BroadcastReciever to get the on know where a download is finished.

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlString));
    request.setDescription(artistString);
    request.setTitle(titleString);
    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC + "/Tunesto", titleString + " - " + artistString + ".mp3");

    Toast.makeText(mainContext, "Downloading " + titleString + " - " + artistString, Toast.LENGTH_SHORT).show();

    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);

Then I use this in my onCreate method to show a toast when the download ended:

onComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String info = "";
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_DESCRIPTION);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_TITLE);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_ID);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_LOCAL_FILENAME);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_MEDIA_TYPE);
            info = info + " - " + intent.getStringExtra(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
            info = info + " - " + intent.getDataString();
            info = info + " - " + intent.getType();
            info = info + " - " + intent.EXTRA_TITLE;
            info = info + " - " + intent.EXTRA_TEXT;

            //Toast.makeText(mainContext, Integer.valueOf(dlCount) + "  Download \"" + titleString + " - " + artistString + "\" completed", Toast.LENGTH_LONG).show();
            Toast.makeText(mainContext, info + "Download completed", Toast.LENGTH_LONG).show();
        }
     };

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

As you can see, i have tried everything to get the description and title inside the info string, but it won't show nothing else than null (tHE toast shows something like ...

Can you please give me a tips on how to retrieve those informations?

Was it helpful?

Solution

Can you please give me a tips on how to retrieve those informations?

Read in the value for EXTRA_DOWNLOAD_ID, by calling getLongExtra() on the broadcast Intent. Then, use that with DownloadManager.Query to retrieve the data for this download. The Cursor you get back from calling query() on DownloadManager will contain values keyed by the COLUMN_ constants.

This blog post has sample code that illustrates this process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top