2013-05-31 54 views
2

我想在一定的毫秒後執行這條指令。如何實現100毫秒睡眠的重試機制

我該如何做到這一點?

try { 
    // Get the typeface 
    MyApp.appTypeFace = Typeface.createFromAsset(getApplication().getAssets(), 
     MyApp.fontName); 
    Log.d("font","in try="+MyApp.fontName); 
    Log.d("font","type face="+MyApp.appTypeFace); 
} 

回答

2
Handler handler; 

     handler=new Handler(); 
     Runnable notification = new Runnable() { 
      @Override 
      public void run() { 
       MyApp.appTypeFace = Typeface.createFromAsset(getApplication().getAssets(), 
     MyApp.fontName); 
    Log.d("font","in try="+MyApp.fontName); 
    Log.d("font","type face="+MyApp.appTypeFace); 

      } 
     }; 
     handler.postDelayed(notification,100); 
+0

ThankQ bro。 它幫了我很多。 – Uday

1

我會用Handler發佈一個Runnable,像這樣:

private Runnable task = new Runnable() { 
    public void run() { 
     // Execute your delayed code 
    } 
}; 

...

Handler handler = new Handler(); 
int millisDelay = 100; 
handler.postDelayed(task, millisDelay); 

的代碼將執行在postDelayed呼叫後100毫秒。