【问题标题】:What is the best way communication between threads?线程之间通信的最佳方式是什么?
【发布时间】:2015-08-10 17:50:19
【问题描述】:

实际上我在 project 内部使用改造,我正在调用 2 个 Aysnc 请求。但我需要在此请求成功后触发其他方法。如何知道这个请求何时完成然后触发其他方法?

【问题讨论】:

    标签: android multithreading retrofit android-handler


    【解决方案1】:

    您可以在 android 中使用异步任务。以下是一些重要的亮点

    创建一个异步任务

    private class MyTask extends AsyncTask<Void, Void, Void> { ... }
    

    它将有 4 个您可以覆盖的方法。

    onPreExecute()
    doInBackground(Params...)
    onProgressUpdate(Progress...)
    onPostExecute(Result)
    

    基本上,您只需将要触发的方法放在 onPostExecute 方法中即可。

    protected void onPostExecute(Long result) {
        //add your method here
    }
    

    更多细节请参考安卓文档 AsyncTask。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-18
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2012-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多