【问题标题】:Java Wait for thread(in another class) to finish before executing code in activityJava 在执行活动中的代码之前等待线程(在另一个类中)完成
【发布时间】:2015-09-03 09:22:41
【问题描述】:

在我的 android 应用程序中,我试图创建一个类似于 ios 委托函数的情况。

(在 ios-> 中)调用执行检查的类,并在完成检查后使用委托将其重定向回视图控制器并执行下一个功能。

这是我的课

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
        }
    }
}

这是我的活动

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
        }
    }
}

问题在于,在调用 Checking 类之后,它会立即执行函数调用下方的 if else。在某些情况下,Checking 类中的检查尚未完成,并显示一个空警报。线程可能会启动也可能不会启动,具体取决于数据库检查。

有人可以为我提供解决此问题的方法吗? 我知道调用 Checking 类后缺少某些内容,但我不太确定应该将其放在那里以达到结果。

提前致谢。

【问题讨论】:

  • 你考虑过使用AsyncTask吗?
  • 只需使用谷歌自己的 volley 库并将结果设置在模型类中即可。这是一种进行网络调用和处理数据的模块化方式
  • 使用AsyncTask运行doChecking函数?
  • 是的,使用 asyncTask,在 async 任务的 doInBackground 方法中做你的网络任务。
  • 一般来说,最好也更简单的方法是使用 Volley。然而,在某些情况下 Volley 并不是最好的,或者根本不容易应用。对于这些情况,您应该使用 AsyncTask,在 doInBackground 方法中执行您的工作。您还可以使用 onPostExecute 方法在完成时执行您想要执行的任何操作。

标签: java android multithreading


【解决方案1】:

你基本上想要做的是点击一个网络服务,然后等到你得到网络服务的响应。

首先,您无需创建自己的Thread 即可访问网络服务。相反,您可以使用AsyncTask

AsyncTask 允许正确且轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。

下面是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
    }

}

如果您想限制用户访问应用程序,直到网络服务被击中。您可以通过其他方法显示对话框,如下所示:

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
    }

}

【讨论】:

    【解决方案2】:

    可以通过以下方式实现,通过在Checking类的构造函数中传递activity并使用它来调用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
       }
    }
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多