Android Open Source - AndroidTasks Task With Dependencies Activity






From Project

Back to project page AndroidTasks.

License

The source code is released under:

Apache License

If you think the Android project AndroidTasks listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.beyondar.android.util.task.example;
// w w  w.  j av a2s  .  c o  m
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.beyondar.android.util.annotation.OnUiThread;
import com.beyondar.android.util.task.BaseTask;
import com.beyondar.android.util.task.TaskExecutor;
import com.beyondar.android.util.task.TaskResult;

public class TaskWithDependenciesActivity extends Activity {

  private Button buttonStart;
  private TextView textView;

  private static int taskId = 0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonStart = (Button) findViewById(R.id.buttonStart);
    textView = (TextView) findViewById(R.id.textView);

    buttonStart.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        textView.setText("");
        CustomTask customTask = new CustomTask(taskId++);
        CustomTask customTask2 = new CustomTask(taskId++);
        
        customTask.setTaskIdToWait(customTask2.getTaskId());
        customTask2.setDelay(true);
        
        textView.append("Adding task " + customTask.getTaskId() + "\n");
        TaskExecutor.getInstance().addTask(customTask);
        textView.append("Adding task " + customTask2.getTaskId() + "\n");
        TaskExecutor.getInstance().addTask(customTask2);
        textView.append("---------------\n");
      }
    });
  }

  private class CustomTask extends BaseTask {

    private boolean delay;
    
    public CustomTask(long id) {
      super(id);
    }
    public void setDelay(boolean delay){
      this.delay = delay;
    }

    @Override
    @OnUiThread
    public void onFinish() {
      textView.append("Task finished: " + getTaskId() + "\n");
    }

    @Override
    public TaskResult runTask() {
      Log.d(getClass().getSimpleName(), "Doing something ");
      if(delay){
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      return null;
    }
  }
}




Java Source Code List

com.beyondar.android.util.annotation.AnnotationsUtils.java
com.beyondar.android.util.annotation.OnUiThread.java
com.beyondar.android.util.task.BaseTask.java
com.beyondar.android.util.task.BaseTimerTask.java
com.beyondar.android.util.task.OnFinishTaskListener.java
com.beyondar.android.util.task.OnFinishTask.java
com.beyondar.android.util.task.OnThreadFromPoolStop.java
com.beyondar.android.util.task.PoolThreads.java
com.beyondar.android.util.task.RunnableTask.java
com.beyondar.android.util.task.SimpleTask.java
com.beyondar.android.util.task.TaskExecutor.java
com.beyondar.android.util.task.TaskResult.java
com.beyondar.android.util.task.Task.java
com.beyondar.android.util.task.ThreadFromPool.java
com.beyondar.android.util.task.Version.java
com.beyondar.android.util.task.example.MainActivity.java
com.beyondar.android.util.task.example.TaskWithDependenciesActivity.java
com.beyondar.android.util.task.example.TaskWithUiThreadAccessActivity.java