Android,异步任务,检查状态?

Android,异步任务,检查状态?,android,Android,这是我的代码: 在onCreate中: new LoadMusicInBackground().execute(); 在我的主课快结束的时候,我有了这个代码 /** Helper class to load all the music in the background. */ class LoadMusicInBackground extends AsyncTask<Void, String, Void> { @Override protected Void d

这是我的代码:

在onCreate中:

 new LoadMusicInBackground().execute();
在我的主课快结束的时候,我有了这个代码

/** Helper class to load all the music in the background. */
class LoadMusicInBackground extends AsyncTask<Void, String, Void> {
    @Override
    protected Void doInBackground(Void... unused) {

        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();

        soundPoolMap.put(A1,
                soundPool.load(GameScreen_bugfix.this, R.raw.a, 1));
        soundPoolMap.put(A3,
                soundPool.load(GameScreen_bugfix.this, R.raw.b, 1));
        soundPoolMap.put(A5,
                soundPool.load(GameScreen_bugfix.this, R.raw.c_s, 1));
        soundPoolMap.put(A6,
                soundPool.load(GameScreen_bugfix.this, R.raw.d, 1));
        soundPoolMap.put(A8,
                soundPool.load(GameScreen_bugfix.this, R.raw.e, 1));
        soundPoolMap.put(A10,
                soundPool.load(GameScreen_bugfix.this, R.raw.f_s, 1));
        soundPoolMap.put(A12,
                soundPool.load(GameScreen_bugfix.this, R.raw.g_s, 1));
        soundPoolMap.put(wrong,
                soundPool.load(GameScreen_bugfix.this, R.raw.wrong2, 1));

        publishProgress("");
        Log.v("SOUNDPOOL", "" + soundPoolMap);
        return (null);
    }

    @Override
    protected void onProgressUpdate(String... item) {
        // text1.setText(item[0]);
    }

    @Override
    protected void onPostExecute(Void unused) {
        //Toast.makeText(GameScreen_bugfix.this, "music loaded!", Toast.LENGTH_SHORT).show();
    }
}
这是行不通的:( 如何检查后台任务是否完成,音乐是否已加载

谢谢!
Ryan

getStatus()
检查
异步任务
是挂起、正在运行还是已完成

LoadMusicInBackground lmib = new LoadMusicInBackground();

if(lmib.getStatus() == AsyncTask.Status.PENDING){
    // My AsyncTask has not started yet
}

if(lmib.getStatus() == AsyncTask.Status.RUNNING){
    // My AsyncTask is currently doing work in doInBackground()
}

if(lmib.getStatus() == AsyncTask.Status.FINISHED){
    // My AsyncTask is done and onPostExecute was called
}

如果您想检查您的操作是否确实成功(即,音乐已成功加载),则需要使用自己的方法来确定只能确定
异步任务中实际线程的状态这是异步编程-您不应该从UI线程进行检查,因为这意味着您正在阻止UI线程(可能正在使用thread.sleep()运行签入循环)

相反,在AsyncTask完成时应该调用您:从它的
onPostExecute()
调用活动中您需要的任何方法


警告:这种方法的缺点是,当后台线程完成时,活动必须处于活动状态。通常情况并非如此,例如,如果执行了back或更改了方向。更好的方法是从
onPostExecute()发送广播
然后感兴趣的活动可以注册接收它。最好的部分是,活动只有在当时处于活动状态时才会接收广播,这意味着多个活动可以注册,但只有活动的活动才会接收广播。

使用侦听器创建asynctask

class ClearSpTask extends AsyncTask<Void, Void, Void> {

    public interface AsynResponse {
        void processFinish(Boolean output);
    }

    AsynResponse asynResponse = null;

    public ClearSpTask(AsynResponse asynResponse) {
        this.asynResponse = asynResponse;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showProgressDialog();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        cleardata();
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        hideProgressDialog();
        asynResponse.processFinish(true);
    }
}

我希望这可以帮助一些人澄清:FINISHED是调用onPostExecute后的状态。如果您在onPostExecute期间检查状态,则状态将运行,那么为什么它会在PostExecute中返回RUNNING,这有点令人惊讶。还值得一提的是,如果通过调用cancel()取消任务方法,它仍将返回运行状态。@user2758776只要“onPostExecute”就应该返回运行状态尚未调用。调用cancel实际上并不会停止线程。它只是告诉线程已对其调用了cancel。@Matthias:捕捉得很好。从源代码上看,这似乎是Honeycomb之前的情况。现在,不是onPostExecute(对象结果),而是onCancelled(对象结果)改为调用,但现在应该设置为FINISHED。看起来您总是需要检查“isCanceled()”以确保安全。
class ClearSpTask extends AsyncTask<Void, Void, Void> {

    public interface AsynResponse {
        void processFinish(Boolean output);
    }

    AsynResponse asynResponse = null;

    public ClearSpTask(AsynResponse asynResponse) {
        this.asynResponse = asynResponse;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showProgressDialog();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        cleardata();
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        hideProgressDialog();
        asynResponse.processFinish(true);
    }
}
new ClearSpTask(new ClearSpTask.AsynResponse() {
        @Override
        public void processFinish(Boolean output) {
            // you can go here   
        }
}).execute();