Search code examples
androidandroid-download-manager

onReceive does'n work properly


I have a download manager that downloads an image on clicking a button. with help of broadcast receivers I will do this. below is my code:

public void myDownloadManager(){

        receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();

                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {

                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);

                        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
//                            download finished successfully
                            Log.e("count downloads", "counting");
                            db.insertDownloadsRows(image_id);

                        }
                    }
                }
            }
        };

        getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

    }

    public void downloadImage(){

        myDownloadManager();

        dm = (DownloadManager) getActivity().getSystemService(getActivity().DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("some uri"));
        request.setDestinationInExternalPublicDir("some directory", name);
        enqueue = dm.enqueue(request);


    }

and downloadImage() is called in button's onClickListener. when I tap the button for the first time, the image will be downloaded once and the Log message shown up once, for the second time when I tap the button, the image will be downloaded once but the Log message shown up twice, and this happens as much as I tap button. why is it this way? how should it be fixed?


Solution

  • That happens because you are registering the receiver multiple times without unregistering it, so you have to do one of two things:

    even register the receiver only once like within your onCreate() method for example (which is absolutely the better solution):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview);
    
        myDownloadManager();
    }
    
    public void downloadImage(){
    
        dm = (DownloadManager) getActivity().getSystemService(getActivity().DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("some uri"));
        request.setDestinationInExternalPublicDir("some directory", name);
        enqueue = dm.enqueue(request);
    
    }
    

    OR call unregister receiver each time you finish dealing with the downloading file:

    public void downloadImage(){
    
        // Un-registering the receiver
        unregisterReceiver(receiver);
    
        myDownloadManager();
    
        dm = (DownloadManager) getActivity().getSystemService(getActivity().DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("some uri"));
        request.setDestinationInExternalPublicDir("some directory", name);
        enqueue = dm.enqueue(request);
    
    }