Android - DownloadManager / BroadcastReceiver called multiple times
Asked Answered
F

2

10

I am using a DownloadManager to handle a download in my application, I would like to notify the user when the download is completed.

I am using the folowing code that is working well

 public void downloaddownload(View v){
        View v2 = (View) v.getParent();
        TextView urlView = (TextView) v2.findViewById(R.id.url);
        String urlString = (String) urlView.getText().toString();
        TextView artistView2 = (TextView) v2.findViewById(R.id.artist);
        final String artistString = (String) artistView2.getText().toString();
        TextView titleView2 = (TextView) v2.findViewById(R.id.title);
        final String titleString = (String) titleView2.getText().toString();

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlString));
        request.setDescription(titleString);
        request.setTitle(artistString);
        // 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 + "/folder", 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);

        onComplete = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                Toast.makeText(mainContext, "Download \" " + titleString + " - " + artistString + "\" completed", Toast.LENGTH_LONG).show();
            }
         };

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


    }

The problem is that the onReceive method is called for previous downloads too.

Let's say I download a.mp3, b.mp3 and c.mp3, when a.mp3 is completed I recieve a.mp3 completed, when b.mp3 is completed I recieve a.mp3 is completed, then a new toast b.mp3 is completed...

How could I prevent this? thank you.

Fatal answered 8/1, 2014 at 3:4 Comment(2)
There are also issues with Download Manager. See also #10853321Hux
in your BroadcastReceiver onReceive methode you need a unregister like Activity.this.unregister(this)Holiday
S
14

Yo are registering a BroadcastReceiver each time you download a file. That means, the second time you download a file, you'll have two receivers registered. You should probably unregister them using unregisterReceiver() after the work is done (probably in onReceive()).

Smarten answered 31/3, 2014 at 11:50 Comment(1)
I had same problem too. tnxRecalesce
G
2
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(downloadId);
Cursor cur = manager.query(query);
    if (cur.moveToFirst()) {
        int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
        if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) {
            titleString=cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_TITLE));}

you should use this to get your title string for individual downloads . because every download have their own id . I know Answer is too late but it may help for others in future...

Gastineau answered 23/4, 2014 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.