Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在“等待”;至于;上传任务完成前的服务循环?_Android_Android Service - Fatal编程技术网

Android 如何在“等待”;至于;上传任务完成前的服务循环?

Android 如何在“等待”;至于;上传任务完成前的服务循环?,android,android-service,Android,Android Service,我已经写了类似于下面提到的程序 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++){

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

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 
            */
        }
  }
}
公共类上传服务扩展服务{
公共上传服务(){
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
for(int i=0;i
我想让循环等到当前上传完成后再进行另一次迭代

您可以通过JML解决这个问题。它只是简单地添加数字。当它达到您想要的数量时,任务将经过一个过程或将完成。下面是如何实现它

在您的
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
        }
  }
}
公共类上传服务扩展服务{
公共上传服务(){
//过程
mObsInt.set(mObsInt.get()+1);//现在mObsInt将是1
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
for(int i=0;i
PS:如果上载进程有一个
AsyncTask
,则放入
mObsInt.set(mObsInt.get()+1)onPostExecute
方法中的code>

希望能有帮助

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

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

您可以在请求队列中添加上传请求,而不是等待。您可以详细说明吗?在Android的ObserveInt可用之前,您需要在gradle中启用数据绑定(请参阅),但OnIntegerChangeListener不作为ObserveInt的一部分存在。您可能实现了中描述的自定义类,并将其标记错误。