如何在以下代码中检查AsyncTask的状态?

时间:2016-02-18 21:12:31

标签: android android-asynctask

public class SplashScreen extends AppCompatActivity {
    ProgressBarHandler progressBarHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        progressBarHandler = new ProgressBarHandler(this);
        startTheDownload();
    }

    public void startTheDownload() {
        progressBarHandler.show();

        SaveDataOffline saveDataOffline = new SaveDataOffline(getApplicationContext());
        saveDataOffline.execute();
        AsyncTaskStopWords asyncTaskStopWords = new AsyncTaskStopWords(getBaseContext());
        asyncTaskStopWords.execute();

        if (saveDataOffline.getStatus() == AsyncTask.Status.FINISHED && asyncTaskStopWords.getStatus() == AsyncTask.Status.FINISHED) {
            progressBarHandler.hide();
            finish();
            Log.i("qwer", "finished");
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            this.startActivity(intent);
        }
    }
}

我尝试了以下代码,但是它没有按照进度条进行操作

1 个答案:

答案 0 :(得分:2)

使用get()

SaveDataOffline saveDataOffline = new SaveDataOffline(getApplicationContext());
saveDataOffline.execute().get();
AsyncTaskStopWords asyncTaskStopWords = new AsyncTaskStopWords(getBaseContext());
asyncTaskStopWords.execute().get();

代码将等到AsyncTask完成后再转到下一行。