0

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

Community
  • 1
  • 1
Luis
  • 25
  • 1
  • 2
  • 8

1 Answers1

0

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.

Matt
  • 690
  • 4
  • 10
  • Seems reasonable... I'm fixing the logic of that invocation as soon as possible and come up with something, anyhow; is the asyncTask well declared or there it would be any mistake with the parameters or any other thing? – Luis Aug 19 '13 at 08:05
  • Did you ever get this sorted? – Matt Oct 03 '13 at 01:13
  • Excuse me for getting so late on solving this issue. I could not solve the AsyncTask issue, but I've found something; I was trying to get asleep inside a method different from the main one, and it seems the sleep won't work there, but if a systemclock sleep or a thread sleep are in the mainloop where "reproducirSecuencia" is invoked, the UI thread does not get blocked, it took a lot of time to me to understand this. Thanks a lot for your efforts Matt! – Luis Oct 08 '13 at 10:10