Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver onReceive() getting called twice

In my app, I am using DownloadManager, for downloading PDF's, which notifies the application via a BroadcastReceiver once the download is completed. My problem is the onReceive() method of BroadcastReceiver is getting called twice. The code is as follows:

In my list adapter, a for loop is run for downloading the selected pdf's. The downloading code is written in another class as follows:

    public static void downloadCheat(final SherlockFragmentActivity activity, final String cheatName, String pathOnServer){

    Request request = new Request(
            Uri.parse(ApplicationConstants.CHEAT_DOWNLOAD_SERVER_URL
                    + "/" + pathOnServer + cheatName + ".pdf"));

    if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        request.setShowRunningNotification(true);
    }
    else {
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }

    final DownloadManager dm = (DownloadManager) activity
            .getSystemService(Context.DOWNLOAD_SERVICE);
    final long enqueue = dm.enqueue(request);

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            long i = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            System.out.println(i);
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                Query query = new 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)) {
                    }
                }
                                    //create custom notification
            }
        }
    };

    activity.registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

I am trying to add notifications for each pdf download. This works perfectly with download managers own internal notification for HoneyComb and above versions but for GingerBread it does not work and hence I have to push my own custom notification. So I need to determine the exact time when the pdf is downloaded completely. As of now I am able to push my own custom notification but the notifications come twice for every pdf download (As onReceive() is getting twice for each pdf). Can anyone please explain why onReceive() is called twice(for every pdf). Is there any workaround for this? Also could someone please recommend how the broadcast receiver can be un-registered in my case here?The above code is not a part of Activity, so I am not sure how to unregister the receiver.

Thanks for stopping by and reading the post.

like image 891
andro Avatar asked Mar 13 '13 00:03

andro


1 Answers

You normally register receivers onResume() and unregister in onPause(). Are you doing so?


I think I may have originally misunderstood what you were trying to do. You should be able to call unregisterReceiver from onReceive. Does this do what you want?

like image 72
yarian Avatar answered Oct 04 '22 05:10

yarian