2011-12-30 9 views
10

sto provando a visualizzare un messaggio ogni 1 minuto !! non stop! ho trovato un esempio che mostra il messaggio solo una volta dopo un ritardo fisso !! puoi aiutare come si può impostare ?? o se usare il timer è meglio come funziona ho bisogno di un esempio !!gestore o timer android

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

Grazie!

+0

Se hai trovato una soluzione che funziona per voi, si prega di accettare la risposta. –

risposta

24

provare questo codice -

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); 
     } 
} 
+1

yaaaaaaah fantastico funziona correttamente !! grazie mille :) –

+0

Grande che ti aiuta. Si prega di accettare anche la risposta se aiuta. – anujprashar

+0

come posso fare questo ??? –

1

È possibile utilizzare TimerTask per this.But quando il dispositivo va dormire non si lavora quindi penso che è possibile utilizzare per questo AlarmManager. riferimento Comunque this link per TimerTask,

codice AlarmManager,

AlarmManager am = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE); 
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       SystemClock.elapsedRealtime(), interval, pendingIntent); 
+0

Ma nel gestore degli allarmi non puoi mostrare il messaggio di toast.Verrà mostrato una nuova attività.ma questo ti sarà utile. –

2
// 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); 

}

Problemi correlati