Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handler or timer android

i'm tryin' to display a msg every 1 min!! non stop! i found exemple that display the msg just one time after a fixed delay!! can you help how can set it?? or if using timer is better how it works i need an exemple!!

public class TimertestActivity extends Activity {
    /** Called when the activity is first created. */

      @Override   
      public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);   
        setContentView(R.layout.main);  
        Handler handler = new Handler();
        handler.postDelayed(
            new Runnable() {
                public void run() {
                    afficher();
                }
            }, 1000L);

      }   

      public void afficher()
      {
          Toast.makeText(getBaseContext(),
                     "test",
                     Toast.LENGTH_SHORT).show();
      }
}

Thanks!

like image 599
manita marwa Avatar asked Dec 30 '11 10:12

manita marwa


2 Answers

Try this code -

public class TimertestActivity extends Activity {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
            afficher();
        }
    };

    /** Called when the activity is first created. */

      @Override   
      public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);   
        setContentView(R.layout.main);  
        runnable.run();
      }   

      public void afficher()
      {
          Toast.makeText(getBaseContext(),
                     "test",
                     Toast.LENGTH_SHORT).show();
          handler.postDelayed(runnable, 1000);
      }
}
like image 195
anujprashar Avatar answered Oct 13 '22 21:10

anujprashar


// Timer using Handler

private final int SPLASH_TIME = 3000;

// Handling splash timer.
private void startSplashTimer() {
    new Handler().postDelayed(
    new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(SplashScreen.this,MainActivity.class));
    }
}, SPLASH_TIME);

}

like image 28
Sanjeev Kumar Avatar answered Oct 13 '22 19:10

Sanjeev Kumar