Alex Rivera | Logout

BroadcastReceiver onReceive() getting called twice

Asked 2013-03-13T00:19:21.477
9

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
            }
        }
    };

    activi
Edit
Report

1 Answer

3

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?

answered 2013-03-13T00:29:06.627

Your Answer