Aller au contenu

[resolu]Probleme de timer


Landroid

Recommended Posts

Salut,

J'ai un programme simple qui affiche un toast toutes les 5 secondes mais je ne comprends pas très bien pourquoi rien ne s'affiche :

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.os.Handler;
import android.os.Message;
import android.content.Context ;

public class main extends Activity {
   /** Called when the activity is first created. */
   protected static final int DEFAULTSECONDS = 5 ;
   protected static final int SECONDPASSEDIDENTIFIER = 0x1337;

   protected boolean running = false;

   protected int mySecondsPassed = 0;

   protected int mySecondsTotal = DEFAULTSECONDS ;

   Thread myRefreshThread = null;

   Handler myTimeUpdateHandler = new Handler()
   {
        public void handleMessage(Message msg) 
        {
                if (msg.what == main.SECONDPASSEDIDENTIFIER) 
                {
                             if (running) 
                             {
                                             mySecondsPassed++;
                                             if(mySecondsPassed == mySecondsTotal)
                                             {
                                                 Context context = getApplicationContext();
                                                 CharSequence text = "Hello toast!";
                                                 int duration = Toast.LENGTH_SHORT;

                                                 Toast toast = Toast.makeText(context, text, duration);
                                                 toast.show();
                                               mySecondsPassed = 0 ;
                                           }

                            }

                            super.handleMessage(msg);
                }
        }
   };

   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       this.myRefreshThread = new Thread(new secondCountDownRunner());
       this.myRefreshThread.start();
   }

   class secondCountDownRunner implements Runnable
   {
              public void run() 
              {
                       while(!Thread.currentThread().isInterrupted())
                       {
                                   Message m = new Message();
                                   m.what = main.SECONDPASSEDIDENTIFIER;
                                   main.this.myTimeUpdateHandler.sendMessage(m);
                                   try
                                   {
                                           Thread.sleep(1000);
                                   } 
                                   catch (InterruptedException e)
                                   {
                                       Thread.currentThread().interrupt();

                                    }

                        }

              }

   }

}

Mon code à pourtant l'air correct ? (Je me suis inspiré du pizza gadget de Plusminus.)

Lien vers le commentaire
Partager sur d’autres sites

Ca y est ca fonctionne.

Deux broutilles : L'un qui etait le runnig à False, j'avais oublié de l'enlever en plus il sert à rien.

Puis le toast qui plantait, voila le code fonctionnel :

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.os.Handler;
import android.os.Message;
import android.content.Context ;

public class main extends Activity {
   /** Called when the activity is first created. */
   protected static final int DEFAULTSECONDS = 5 ;
   protected static final int SECONDPASSEDIDENTIFIER = 0x1337;

   protected int mySecondsPassed = 0;

   protected int mySecondsTotal = DEFAULTSECONDS ;

   Thread myRefreshThread = null;



   Handler myTimeUpdateHandler = new Handler()
   {
        public void handleMessage(Message msg) 
        {
                if (msg.what == main.SECONDPASSEDIDENTIFIER) 
                {
                                             mySecondsPassed++;
                                             if(mySecondsPassed == mySecondsTotal)
                                             {
                                                 Toast t = Toast.makeText(main.this, "Suite...", Toast.LENGTH_SHORT);
                                                  t.show();
                                               mySecondsPassed = 0 ;
                                           }
                                             super.handleMessage(msg);
                }
        }
   };

   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
           Toast t = Toast.makeText(main.this, "La suite dans 5 secondes...", Toast.LENGTH_SHORT);
            t.show();
      this.myRefreshThread = new Thread(new secondCountDownRunner());
      this.myRefreshThread.start();
   }

   class secondCountDownRunner implements Runnable
   {
              public void run() 
              {
                       while(!Thread.currentThread().isInterrupted())
                       {
                                   Message m = new Message();
                                   m.what = main.SECONDPASSEDIDENTIFIER;
                                   main.this.myTimeUpdateHandler.sendMessage(m);
                                   try
                                   {
                                           Thread.sleep(1000);
                                   } 
                                   catch (InterruptedException e)
                                   {
                                       Thread.currentThread().interrupt();

                                    }

                        }

              }

   }

}

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...