Search code examples
androidbroadcastreceiverandroid-download-managerlocalbroadcastmanager

onReceive not being called when BroadcastReceiver is registered using LocalBroadcastManager


I've created a BroadcastReceiver to receive ACTION_DOWNLOAD_COMPLETE when my app starts downloading something using DownloadManager. As I want to capture ACTION_DOWNLOAD_COMPLETE only when downloading is started from my app, I've used LocalBroadcastManager.

But onReceiver is not being called at all. DownloadManager app shows that download is complete but onReceive is not triggered. When I use registerReceiver it works as expected. But this would let app being notified even if downloading is started by some other app. So LocalBroadcastManager is desired.

MainActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        downloadReceiver = new DownloadReceiver();

        LocalBroadcastManager.getInstance(this).registerReceiver(downloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));           

        downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        if(FileHelper.isStorageAvailable()) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://example.com/image.jpg"));
            downloadManager.enqueue(request);
        }
    }
    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(downloadReceiver);
        super.onPause();
    }

DownloadReciever

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

            long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadId);
            Cursor c = downloadManager.query(query);

            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {                        
                    String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));                 
                    Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

It simply don't call onRecieve as it should. Point out if I'm doing something wrong here. Been stuck here for quite time now. I can't use registerReceiver as I need to track download complete action only if my app starts downloading.


Solution

  • As I want to capture ACTION_DOWNLOAD_COMPLETE only when downloading is started from my app, I've used LocalBroadcastManager.

    That is not going to work. DownloadManager does the download in a separate process, and it will use a system broadcast. The only broadcasts that you can receive via LocalBraodcastManager are the ones that you broadcast via LocalBroadcastManager.