[英]Having trouble with Asynctask in android
我正在為Android開發某種游戲,為了實現此目的,我使用了一個Activity內的SurfaceView。
有時有時需要暫停,因此Surfaceview實際上在畫布上繪制了一些東西,然后根據存儲在Vector中的一些整數安全地等待並繪制了其他東西。
最初,我試圖通過使用Thread.sleep()和Systemclock.sleep()來使此暫停,這似乎毫無用處,因為線程總是被阻塞,因此即使正確調用了繪圖方法,也不會顯示任何更改。
在這里,我看到使用Asynctask做睡眠工作,然后升起一個標志應該是完成事情的好主意。
所以我這樣做了,但是似乎從未調用過onPostExecute,所以我的標志從未升起...
關於如何進行的任何想法?
這是我的第一個android應用程序,因此請您盡量仁慈和寬容。
非常感謝。
這是暫停方法:
public void pausar(){
Log.e("pausar", "entro a pausar");
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Log.e("pausar", "Empiezo a dormir");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("pausar", "Acaba el sleep");
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
despierta = true;
Log.e("pausar", "¡¡¡¡¡¡¡¡¡¡¡¡¡¡DESPIERTO!!!!!!!!!!!");
}
};
if(!llamaPausa){
task.execute();
llamaPausa = true;
}
}
這是需要暫停的序列播放器方法:
public void reproducirSecuencia(final Canvas canvas){
reproduciendo = true;
//TODO: HACER QUE ESTO FUNCIONE!!
Log.e("reproducirSecuencia", "Entro a reproducir");
int i = 0;
while(i < secuencia.size()){
Object o = secuencia.elementAt(i);
int num = 0;
if (o instanceof Integer) {num = (Integer) o;}
reproducirSonido(num);
repId = num;
onDraw(canvas);
//Log.e("reproducirSecuencia", "repId = " + repId);
//Log.e("reproducirSecuencia", "Invoco a pintarPiezas");
i++;
if(!despierta){
pausar();
Log.e("Repsec", "despierta = " + despierta);
}
llamaPausa = true;
despierta = false;**
//SystemClock.sleep(1000);
//try {Thread.sleep(1000);}
//catch (InterruptedException e) {e.printStackTrace();}
}
reproduciendo = false;
}
這是logcat:
08-18 22:21:54.050 805-904 / com.example.simondeluxe E / onDraw:輸入onDraw desde repsec
08-18 22:21:54.060 805-904 / com.example.simondeluxe E / pausar:輸入一個pausar
08-18 22:21:54.060 805-904 / com.example.simondeluxe E / Repsec:despierta = false
08-18 22:21:54.070 805-904 / com.example.simondeluxe E / onDraw:輸入onDraw desde repsec
08-18 22:21:54.070 805-904 / com.example.simondeluxe E / pausar:輸入一個pausar
08-18 22:21:54.070 805-904 / com.example.simondeluxe E / Repsec:despierta = false
08-18 22:21:54.080 805-904 / com.example.simondeluxe E / onDraw:輸入onDraw desde repsec
08-18 22:21:54.080 805-904 / com.example.simondeluxe E / pausar:輸入一個pausar
08-18 22:21:54.080 805-904 / com.example.simondeluxe E / Repsec:despierta = false
08-18 22:21:54.090 805-904 / com.example.simondeluxe E / onDraw:輸入一個onDraw desde repsec
08-18 22:21:54.090 805-904 / com.example.simondeluxe E / pausar:輸入一個pausar
08-18 22:21:54.090 805-904 / com.example.simondeluxe E / Repsec:despierta = false
由於onPausar()
中的onPausar()
代碼,您的AsyncTask
將永遠不會執行:
if(!llamaPausa){
task.execute();
llamaPausa = true;
}
llamaPausa
的值始終為true,在您的代碼中沒有任何地方將其設置為false,因此可以執行前面的語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.