2011-02-10 19 views

Respuesta

10

Puede llamar a Runnable utilizando el método postDelayed() del controlador.

He aquí un ejemplo (http://developer.android.com/resources/articles/timed-ui-updates.html):

private Handler mHandler = new Handler(); 

... 

OnClickListener mStartListener = new OnClickListener() { 
    public void onClick(View v) { 
      mHandler.postDelayed(mUpdateTimeTask, 100); 
    } 
}; 

private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     // do what you need to do here after the delay 
    } 
}; 

Apoyos a @mad para hacerlo bien la primera vez .

4

Puede usar el método postDelayed(Runnable action, long delayMillis) de View para agregar un Runnable a la cola de mensajes que se ejecutará después de un retraso (aproximado).

+0

Esto permitirá el comportamiento deseado sin bloquear el subproceso de interfaz de usuario. –

28

Use una llamada postDelayed() con un ejecutable que inicia su actividad. Un código de ejemplo podría ser

//will care for all posts 
    Handler mHandler = new Handler(); 

    //the button's onclick method 
    onClick(...) 
    { 
     mHandler.postDelayed(mLaunchTask,MYDELAYTIME); 
    } 

    //will launch the activity 
    private Runnable mLaunchTask = new Runnable() { 
     public void run() { 
      Intent i = new Intent(getApplicationContext(),MYACTIVITY.CLASS); 
      startActivity(i); 
     } 
    }; 

Tenga en cuenta que esto permite que la interfaz permanece reactiva. A continuación, debe tener cuidado de eliminar el oyente onclick de su botón.

11

Usar este código

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      final Intent mainIntent = new Intent(CurrentActivity.this, SecondActivity.class); 
      LaunchActivity.this.startActivity(mainIntent); 
      LaunchActivity.this.finish(); 
     } 
    }, 4000); 
0

A veces, u necesidad de hacerlo cada vez que el proceso de aplicación está muerto o no. En ese caso, no puede usar el manejo de mensajes ejecutables o dentro de su proceso. En este caso, puede usar AlarmManager para esto. Espero que esto ayuda a nadie ejemplo:

Intent intent = new Intent(); 
... 

PendingIntent pendingIntent = PendingIntent.getActivity(<your context>, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
mgr.set(AlarmManager.RTC, <your delay>, pendingIntent); 
0
runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      new Handler().postDelayed(new Runnable(){ 
       @Override 
       public void run() { 
        Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
        startActivity(intent); 
       } 
      }, 4000); 
     } 
    }); 
0

probar este pedazo de código

new Timer().schedule(new TimerTask() {   
    @Override 
    public void run() { 

     // run AsyncTask here.  


    } 
}, 3000); 
Cuestiones relacionadas