简体   繁体   English

检索Koush的自定义Future Android Ion

[英]Retrieving custom Future Android Ion by Koush

am new to Android. 是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. 我在我的项目中使用https://github.com/koush/ion中的Ion。 Uploads and downloads work well but when it comes to retrieving a specific custom Future after resuming the app I get stuck. 上传和下载效果很好,但是在恢复应用后检索特定的自定义Future时,我陷入了困境。 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. 解决方案是在创建返回Future回调的流程的过程中实现广播接收器。 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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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