2011-09-28 60 views
35

这是我的代码:Android,AsyncTask,检查状态?

在的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(); 
    } 
} 

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

music_load_status=LoadMusicInBackground.getStatus() 

和不工作:( 如何检查,如果后台任务就完成了,音乐已经装?

谢谢!
Ryan

回答

156

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 
} 

如果你想检查你的行动实际上已成功(即音乐已成功加载),那么你需要拿出你自己的方法确定。 getStatus()只能确定AsyncTask中实际线程的状态。

+0

谢谢!将检查出来! – Ryan

+11

澄清:FINISHED是onPostExecute被调用后的状态。如果您检查状态德宁onPostExecute状态将运行 – user123321

+0

那么为什么它返回postexcute RUNNING,其令人惊讶的 – Sameer

17

这是异步编程 - 你不应该从UI线程检查,因为这意味着你是阻塞UI线程(大概在运行循环检查与Thread.sleep()方法?)。

相反时的AsyncTask完成后,你应该叫:从它的onPostExecute()调用任何你需要的方法活动。

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

2

与听众

创建的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); 
    } 
} 

和使用的AsyncTask

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

我希望这可以帮助一些人