简体   繁体   中英

Retrieving custom Future Android Ion by Koush

am new to Android. Sorry if this question is too simple. I have tried searching for a solution for weeks now. I am using Ion from https://github.com/koush/ion in my project. Uploads and downloads work well but when it comes to retrieving a specific custom Future after resuming the app I get stuck. I want to retrieve a single operation say an upload and stop it without affecting other uploads or vice versa for downloads.

The solution was to implement a broadcast receiver within creation of the process that returns a Future callback. This will allow you to cancel/stop its operation by just triggering the broadcast receiver

 // set broadcast listeners
    BroadcastReceiver broadcast = new BroadcastReceiver() {

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

            String cancelled_msg_time = intent.getStringExtra("time");

            // check if this download was cancelled
            if (my_time.equals(cancelled_msg_time)) {

                if (upload_started) {
                    // cancel asynctask
                    asycProcess.cancel(true);
                }

                // set flag as cancelled
                cancelled = true;
            }
        }
    };

    // register the broadcast receiver to cancel downloads
    IntentFilter intent_filter = new IntentFilter();
    intent_filter.addAction("CANCEL_UPLOAD");
    context.registerReceiver(broadcast, intent_filter);

Then your execution should somehow check status if it has been cancelled before proceeding

 if (!cancelled) {
            file_progress_handler = new FileProgressHandler();
            // proceed with upload
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM