Question

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.

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.

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.

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.

So I did that but it seems that onPostExecute is never called, so my flag is never risen...

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.

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:

08-18 22:21:54.050 805-904/com.example.simondeluxe E/onDraw: Entro a 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/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/pausar: entro a 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: Entro a 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/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/pausar: entro a pausar

08-18 22:21:54.090 805-904/com.example.simondeluxe E/Repsec: despierta = false

Was it helpful?

Solution

Your AsyncTask will never execute because of this piece of code here in onPausar():

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top