关于在Android中使用大量GUI更新进行编码的概念(Gui Handlers)

时间:2017-02-09 05:56:52

标签: java android android-asynctask background-process conceptual

我有以下伪代码:

MySteps(){
  //Step A
  if (conditionA) {
    showwaitdialog(messageA); // shows wait dialog, so User knows app is running
    longrunnerA; // long running task, like downloading from internet.
    dismissdialog // closes the wait dialog.
  }
  //Step B
  if (conditionB && stepAfinished) { //real if not elseif
    showwaitdialog(messageB); // shows wait dialog
    longrunnerB; // long running task, like downloading from internet.
    dismissdialog
  }  
  //Step C
  if (conditionC && stepDfinished) { //real if not elseif
    showwaitdialog(messageC); // shows wait dialog
    longrunnerC; // long running task, like downloading from internet.
    dismissdialog
  }  
  if (!conditionA && !conditionB && !conditionC && allStepsfinished){
    startStepD();
  }
}

现在我想在我的Android App中实现这个逻辑: 根据我的知识,我会将其编码为以下(也称为pseodocode),这导致非常难看的代码:

HandlerA(){
  dismissDialog();
  if(ConditionB){
    showwaitdialog(messageB); // shows wait dialog
    new BackgroundTask {
      longrunnerB; // long running task, like downloading from internet.
      callHandlerB();
    }
  }
}

HandlerB(){
  dismissDialog();
  if(ConditionC){
    showwaitdialog(messageC); // shows wait dialog
    new BackgroundTask {
      longrunnerC; // long running task, like downloading from internet.
      callHandlerC();
    }
  }
}

HandlerC(){
  dismissDialog();
  if(!ConditionA && !ConditionB && !ConditionC){
    new BackgroundTask {
      startStepD();
    }
  }
}


MySteps(){ // not correct name, because it calls only step A. I cant see,that there is a lot other stuff triggered. 
  showwaitdialog(messageA); // shows wait dialog
  new BackgroundTask(){
    longrunnerA(); // long running task, like downloading from internet.
    call HandlerA();
  }
}

此代码分布在不同的位置,方法“MySteps”的名称错误,因为它只执行StepA。

我如何编写更紧凑,更清晰的代码,因此我将拥有一个方法MySteps(),它集中了整个逻辑,GUI更新将正常工作?

*更新*

使用处理程序进行编码与使用GOTO语句进行编码类似。每个程序员都知道,我们应该避免使用GOTO语句。这就是为什么我在寻找更好的编码概念来实现上面的示例逻辑。

1 个答案:

答案 0 :(得分:0)

解决方案

我认为这个伪代码代码可以使用AsyncTask

如果您的需要功能类似于下载文件,那么这可能是更好的选择。

<强> ExampleTask

public class ExampleTask extends AsyncTask<URL, Void, Boolean> {

    private ProgressDialog dialog;
    private Context context;
    private String message;

    public ExampleTask(Context context, String message)
    {
        this.context = context;
        this.dialog =new ProgressDialog(context);
        this.message = message;
    }

    protected Boolean doInBackground(URL... urls) {
        int count = urls.length;
        showDialog();

        for (int i = 0; i < count; i++) {

            // implement download target url[i]
            if (isCancelled()) return false;
        }

        dismissDialog();
        return true;
    }

    private void showDialog()
    {
        // show dialog
    }

    private void dismissDialog()
    {
        // dissmiss dialog
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);

        //implements result handling
    }
}

<强> ExecuteTask

new ExampleTask(this, "message").execute(url1, url2, url3);