2011-07-15 10 views

Respuesta

18

Utilice un controlador y envíe un mensaje simple o ejecutable usando un método como postDelayed().

Por ejemplo, definir un objeto Handler para recibir mensajes y Runnables:

private Handler mHandler = new Handler(); 

Definir un Ejecutable:

private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     // Do some stuff that you want to do here 

    // You could do this call if you wanted it to be periodic: 
     mHandler.postDelayed(this, 5000); 

     } 
    }; 

Causa la Ejecutable para ser enviado a la Handler después de un retraso especificado en ms :

mHandler.postDelayed(mUpdateTimeTask, 1000); 

Si no quiere la complejidad de se Al asignar un Ejecutable al Manejador, también puede enviar un mensaje, incluso un mensaje vacío, para mayor simplicidad, usando el método sendEmptyMessageDelayed().

+0

gracias! Acabo de agregar 'new Handler(). sendEmptyMessageDelayed (1, 2500);' sin embargo, no sé qué representa el valor 'int what' – austin

+2

El enlace que proporcionó ya no está disponible ... – amalBit

+0

OK, enlace eliminado. Creo que la respuesta es bastante autónoma, ya que aún es. – Trevor

0

llamada al método retraso de un contexto estático

public final class Config { 
    public static MainActivity context = null; 
} 

En MainActivity:

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    ... 
    Config.context = this; 
    ... 
} 

... 

public void execute_method_after_delay(final Callable<Integer> method, int millisec) 
{ 
    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       method.call(); 
      } 
      catch (Exception e) { 

      } 
     } 
    }, millisec); 
} 

De cualquier clase utilizando métodos estáticos:

private static void a_static_method() 
{ 

    int delay = 3000; 
    Config.context.execute_method_after_delay(new Callable<Integer>() { 
     public Integer call() { 
      return method_to_call(); 
     } 
    }, delay); 


} 

public static Integer method_to_call() 
{ 
    // DO SOMETHING 
Cuestiones relacionadas