这是我的代码: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