Android,AsyncTask,检查状态?

Rya*_*yan 38 android

这是我的代码:

在onCreate:

 new LoadMusicInBackground().execute();
Run Code Online (Sandbox Code Playgroud)

然后在我的主要课程结束时,我有这个代码

/** 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果音乐没有加载我得到一个nullpointer异常,看着我看到有一个getStatus()的文档,但我尝试过这样的事情:

music_load_status=LoadMusicInBackground.getStatus()
Run Code Online (Sandbox Code Playgroud)

这不起作用:(如何检查后台任务是否完整且音乐已加载?

谢谢!
瑞安

Dee*_*eeV 164

getStatus()检查是否AsyncTask正在等待,正在运行或已完成.

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
}
Run Code Online (Sandbox Code Playgroud)

如果你想检查你的动作是否真的成功(即音乐成功加载),那么你需要提出自己的方法来确定.在getStatus()只能确定在实际线程的状态AsyncTask.

  • 澄清:FINISHED是调用PostExecute后的状态.如果您在onPostExecute上检查状态,则状态将为RUNNING (12认同)
  • 值得一提的是,如果通过调用cancel()方法取消任务,它仍将返回RUNNING状态. (4认同)

Pet*_*ego 17

这是异步编程 - 您不应该从UI线程检查,因为这意味着您正在阻止UI线程(可能是使用Thread.sleep()运行检入循环?).

相反,当AsyncTask完成时你应该被调用:从它onPostExecute()调用你需要的Activity中的任何方法.

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


Ari*_*ael 6

使用侦听器创建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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用Asynctask

new ClearSpTask(new ClearSpTask.AsynResponse() {
        @Override
        public void processFinish(Boolean output) {
            // you can go here   
        }
}).execute();
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助一些人