为什么我的AsyncTask.SERIAL_EXECUTOR无法正常工作?

时间:2014-05-26 06:45:19

标签: java android android-asynctask

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import edu.self.debopam.android.asyncsycnhronize.R;

public class MainActivity extends Activity implements OnClickListener, OnTaskCompleted{
    private Button btnInitiate;
    private Integer counter = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnInitiate = (Button) findViewById(R.id.btnInitiate);
        btnInitiate.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.btnInitiate:
            initProcess();
            break;
        }

    }

    private void initProcess(){
        CounterAsyncTask1 counterAsyncTask1 = new CounterAsyncTask1(this);
        counterAsyncTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, counter);
        Log.i("initProcess", String.valueOf(counter));
        CounterAsyncTask2 counterAsyncTask2 = new CounterAsyncTask2();
        counterAsyncTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, counter);
    }
    @Override
    public void onTaskCompleted(Integer counterValue) {
        this.counter = counterValue;
        Log.i("Listener fired", String.valueOf(this.counter));
    }
}

class CounterAsyncTask1 extends AsyncTask<Integer, Void, Integer>{

    OnTaskCompleted onTaskCompleted;

    public CounterAsyncTask1(OnTaskCompleted onTaskCompleted) {
        this.onTaskCompleted = onTaskCompleted;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Integer doInBackground(Integer... counters) {
        int counter = counters[0];
        for(counter = 1 ; counter <= 20 ; counter++){
            Log.i("CounterAsyncTask1", String.valueOf(counter));

        }
        return counter;
    }

    @Override
    protected void onPostExecute(Integer result) {
        onTaskCompleted.onTaskCompleted(result);
    }

}


class CounterAsyncTask2 extends AsyncTask<Integer, Void, Integer>{


    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Integer doInBackground(Integer... counters) {
        Log.i("initProcess", String.valueOf(counters[0]));
        int counter = counters[0];
        if(counter == 0)
            counter = 0;
        for(counter = 1 ; counter <= 100 ; counter++){
            Log.i("CounterAsyncTask2", String.valueOf(counter));
        }
        return counter;
    }

    @Override
    protected void onPostExecute(Integer result) {
    }

}

interface OnTaskCompleted{
    void onTaskCompleted(Integer counterValue);
}

关于我做了什么的一点描述。我已经实现了一个侦听器接口来设置由我的第一个AsyncTask增加的值,以供我的secon AsyncTask使用。但我仍然看到我的第二个AsyncTask似乎没有工作。

似乎我错过了一些东西。但找不到任何东西。没有线索。需要帮助。

需要看的是......在Asynctask的计数器从1到20执行后,下一个AsyncTask将从21开始到100。

0 个答案:

没有答案