简体   繁体   English

在Android中遇到Asynctask的问题

[英]Having trouble with Asynctask in android

I'm developing some sort of game for android, in order of fulfilling this purpose I'm using a SurfaceView which is hold inside an Activity. 我正在为Android开发某种游戏,为了实现此目的,我使用了一个Activity内的SurfaceView。

There are sometimes when a pause is required so the Surfaceview actually draws something in the canvas, then it waits and draws some other thing secuencially according to some integers that are stored inside a Vector. 有时有时需要暂停,因此Surfaceview实际上在画布上绘制了一些东西,然后根据存储在Vector中的一些整数安全地等待并绘制了其他东西。

At first I tried to get this pause by using Thread.sleep(), and Systemclock.sleep(), and it seems to be quite useless because the thread got always blocked so even if the drawing method is called properly no changes were displayed. 最初,我试图通过使用Thread.sleep()和Systemclock.sleep()来使此暂停,这似乎毫无用处,因为线程总是被阻塞,因此即使正确调用了绘图方法,也不会显示任何更改。

Here I saw that using an Asynctask doing the sleeping job and then raising a flag should be a good idea to get things done. 在这里,我看到使用Asynctask做睡眠工作,然后升起一个标志应该是完成事情的好主意。

So I did that but it seems that onPostExecute is never called, so my flag is never risen... 所以我这样做了,但是似乎从未调用过onPostExecute,所以我的标志从未升起...

Any ideas of how to proceed? 关于如何进行的任何想法?

This is my first android app so please be kind and as pacient as you can with your answers. 这是我的第一个android应用程序,因此请您尽量仁慈和宽容。

Thanks a lot. 非常感谢。

Here is the pause method: 这是暂停方法:

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;
    }
}

And this one is the sequence player method where the pause is required: 这是需要暂停的序列播放器方法:

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;
}

Here is the logcat: 这是logcat:

08-18 22:21:54.050 805-904/com.example.simondeluxe E/onDraw: Entro a onDraw desde repsec 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: entro a pausar 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.060 805-904 / com.example.simondeluxe E / Repsec:despierta = false

08-18 22:21:54.070 805-904/com.example.simondeluxe E/onDraw: Entro a onDraw desde repsec 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: entro a pausar 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.070 805-904 / com.example.simondeluxe E / Repsec:despierta = false

08-18 22:21:54.080 805-904/com.example.simondeluxe E/onDraw: Entro a onDraw desde repsec 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: entro a pausar 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.080 805-904 / com.example.simondeluxe E / Repsec:despierta = false

08-18 22:21:54.090 805-904/com.example.simondeluxe E/onDraw: Entro a onDraw desde repsec 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: entro a pausar 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 08-18 22:21:54.090 805-904 / com.example.simondeluxe E / Repsec:despierta = false

Your AsyncTask will never execute because of this piece of code here in onPausar() : 由于onPausar()中的onPausar()代码,您的AsyncTask将永远不会执行:

if(!llamaPausa){
        task.execute();
        llamaPausa = true;
    }

The value of llamaPausa is always true, and I see nowhere in your code where it gets set to false, so the previous statement can execute. llamaPausa的值始终为true,在您的代码中没有任何地方将其设置为false,因此可以执行前面的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM