【问题标题】:how to wait in "for" loop of service until uploadingTask is Complete?如何在“for”服务循环中等待直到 uploadingTask 完成?
【发布时间】:2018-03-13 12:34:11
【问题描述】:

我已经编写了类似于下面提到的程序的程序。

public class UploadingService extends Service {

public UploadingService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        for(int i = 0; i < mArrayUri.size(); i++){
            *********
            //firebase uploading codes
            ************
            /*what code  ??? wait until uploading 
            is complete than for loop iterate new iteration 
            */
        }
  }
}

我希望 for 循环等到当前上传完成后再进行另一个迭代。

【问题讨论】:

标签: android android-service


【解决方案1】:

“等待”另一个任务完成确实是您想要避免的事情,尤其是在 android 上。 您应该检查异步编程的基础知识,但是有很多不同的实现方式,很难只给出一个方向。 基本上,您的任务线程应该在完成时发出通知,这将触发进一步的处理,无论是否修改了 UI。 你可以使用事件总线、post to handler 机制、RX 模式、广播 Intent,当然还有 asynctask 等等......

例如,您可以使用异步任务将您的内容上传到列表中。它在另一个线程中执行,但您始终可以在每次上传之间提供反馈以刷新 UI(进度条)。这对于应用程序来说是非阻塞的,这将允许用户停止任务。 但是,您需要特别注意内存泄漏和配置更改;)

【讨论】:

    【解决方案2】:

    您可以通过 JML 使用 ObservableInteger 解决此问题。它所做的只是简单地添加数字。当它达到您想要的数量时,任务将被处理或完成。以下是如何实现它。

    在您的 OnCreate 中,您只需调用将等待发生某事的侦听器

    private ObservableInteger mObsInt;
    
    //Listener
    mObsInt = new ObservableInteger();
    mObsInt.set(0);
    
    mObsInt.setOnIntegerChangeListener(new OnIntegerChangeListener()
    {
        @Override
        public void onIntegerChanged(int newValue)
        {
            if (mObsInt.get()==1) {
                Log.e("Uploading Process finished"," mObsInt 1");
                Log.e("Download1"," Finished first process ");
    
                //Here you can launch your loop method
            }
    
            if (mObsInt.get()==2) {           
                //Here it will tell you that the loop has finished    
            }
        }
    });
    

    所以,现在你只需要告诉mObsInt 什么时候递增,所以在完成UploadingService 之后它将是1,然后在循环之后它将是2。

    为此,只需添加这一行

    mObsInt.set(mObsInt.get()+1);
    

    实现它看起来像这样:

    public class UploadingService extends Service {
    
    public UploadingService() {
    
    //process
    
    mObsInt.set(mObsInt.get()+1); //now mObsInt will be 1 
    
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            for(int i = 0; i < mArrayUri.size(); i++){
                *********
                //firebase uploading codes
                ************
                /*what code  ??? wait until uploading 
                is complete than for loop iterate new iteration 
                */
       mObsInt.set(mObsInt.get()+1); //Now mObsInt will be 1 + 1 = 2 , so the code in your onCreate will manage what to do
            }
      }
    }
    

    PS:如果 uploadingProcess 有 AsyncTask ,请将 mObsInt.set(mObsInt.get()+1); 放入您的 onPostExecute 方法中

    希望对你有帮助

    愉快的编码

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2020-11-10
    • 2022-01-04
    相关资源
    最近更新 更多