這是我的代碼: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
謝謝!將檢查出來! – Ryan
澄清:FINISHED是onPostExecute被調用後的狀態。如果您檢查狀態德寧onPostExecute狀態將運行 – user123321
那麼爲什麼它返回postexcute RUNNING,其令人驚訝的 – Sameer