简体   繁体   English

Java在活动中执行代码之前等待线程(在另一个类中)完成

[英]Java Wait for thread(in another class) to finish before executing code in activity

In my android application I am trying to create a situation similar to ios delegate function. 在我的Android应用程序中,我试图创建一种类似于ios委托功能的情况。

(in ios->)where a class that perform the checking is called and after finish checking it will be redirected back using delegate to viewcontroller and perform next function. (在ios->中),其中将调用执行检查的类,并在完成检查后使用委托将其重定向回viewcontroller并执行下一个功能。

Here is my Class 这是我的课

public class Checking{
    private boolean flag;

    public boolean getFlag(){
        return flag;
    }

    public void checkFunction(){
        //..... check database

        if(need to do call webservice){
            Thread thread = new Thread(){
                @Override
                public void run(){
                    // Perform webservice calling
                }
            };
            thread.start();
        }
        else{
            //end
        }
    }
}

Here is my Activity 这是我的活动

public class ActivityA extends Activity{
    @Override
    public void onResume(){
        doChecking();
    }

    public void doChecking(){
        Checking check = new Checking();
        check.checkFunction();

        // should finish preform checking in Checking class before proceed
        if(check.getFlag()){
            // perform next function
        }
        else{
            // show alert
        }
    }
}

Problem with this is that right after calling the Checking class it straight away perform the if else below the function call. 问题是,在调用Checking类之后,它立即在函数调用下面立即执行if if else。 Which in some situation the check in Checking class have not finished and an empty alert is shown. 在某些情况下,“检查”类中的检查尚未完成,并且显示为空警报。 The thread might or might not start depending on the database checking. 线程是否启动取决于数据库检查。

Can someone provide me a solution to overcome this? 有人可以为我提供解决方案吗? I know something is missing after calling the Checking class but I am not quite sure what to put it there in order to achieve the result. 我知道在调用Checking类后缺少了一些东西,但我不太确定将其放置在什么地方才能获得结果。

Thanks in advance. 提前致谢。

What you basically want to do is Hit a web service and then wait till you get the response of web service. 您基本上想要做的是单击Web服务,然后等待直到获得Web服务的响应。

First thing, you don't need to create your own Thread t hit web service. 首先,您不需要创建自己的Thread Web服务。 Instead you can use AsyncTask 相反,您可以使用AsyncTask

AsyncTask enables proper and easy use of the UI thread. 通过AsyncTask,可以正确,轻松地使用UI线程。 This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 此类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。

Below is code of AsyncTask 下面是AsyncTask代码

class MyAsync extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
          //Hit your web service here
    }

    @Override
    protected void onPostExecute(final Void unused) {
          //Process the result here
    }

}

If you want to restrict user from accessing the app till web service is hit. 如果要限制用户访问Web服务之前访问应用程序。 You can show dialog from another method, like below: 您可以通过其他方法显示对话框,如下所示:

class MyAsync extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {
        loadingDialog.setMessage("Please wait...");
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
          //Hit your web service here
    }

    @Override
    protected void onPostExecute(final Void unused) {
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        } 
        //Process the result here
    }

}

It can be implemented in the following way, by passing the activity in the Checking class's construtor and using it to call showAlert() function in the ActivityA class. 可以通过以下方式来实现它:将活动传递给Checking类的构造函数,并使用它来调用ActivityA类中的showAlert()函数。

public class Checking{
 Activity activity_;

public Checking(Activity activity){
   this.activity_ = activity;
}

private boolean flag;

public boolean getFlag(){
    return flag;
}

public void checkFunction(){
    //..... check database

    if(need to do call webservice){
        Thread thread = new Thread(){
            @Override
            public void run(){
                // Perform webservice calling
                //after all the process
                 Handler handler = new Handler();
                 handler.post(new Runnable{
                  @Override
                  public void run(){
                    (MyActivity) activity_.showAlert();
                  }});
            }
        };
        thread.start();
    }
    else{
        //end
    }
}
}

public class ActivityA extends Activity{
@Override
public void onResume(){
    doChecking();
}

public void doChecking(){
    Checking check = new Checking(ActivityA.this);
    check.checkFunction();
}

public void showAlert(){
      // should finish preform checking in Checking class before proceed
   if(check.getFlag()){
        // perform next function
   }else{
          // show alert
   }
}
}    

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

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