Đây là mã của tôi:Android, AsyncTask, hãy kiểm tra trạng thái?
Trong onCreate:
new LoadMusicInBackground().execute();
Sau đó, vào cuối lớp học chính của tôi, tôi có mã này
/** 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();
}
}
Nếu âm nhạc đã không được nạp Tôi nhận được một nullpointer ngoại lệ, nhìn vào các tài liệu tôi thấy có một getStatus() nhưng tôi đã thử một cái gì đó như thế này:
music_load_status=LoadMusicInBackground.getStatus()
và điều đó không hoạt động: ( Làm cách nào để kiểm tra xem tác vụ nền đã hoàn thành chưa và nhạc đã được tải chưa?
Cảm ơn!
Ryan
Cảm ơn! Sẽ kiểm tra xem nó ra! – Ryan
Để làm rõ: HOÀN THÀNH là trạng thái SAU onPostExecute được gọi. Nếu bạn kiểm tra trạng thái thời gian trênPostExecute trạng thái sẽ được RUNNING – user123321
thì tại sao nó trở lại RUNNING sau khi postexcute, bit của nó đáng ngạc nhiên – Sameer