2

我想在几毫秒后执行这条指令。

我怎样才能做到这一点?

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

3 回答 3

2

我会使用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 毫秒执行。

于 2013-05-31T12:39:07.747 回答
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);
于 2013-05-31T12:57:22.867 回答
1

我通常使用CountDownTimer:

http://developer.android.com/reference/android/os/CountDownTimer.html

CountDownTimer timer = new CountDownTimer(100, 100) {

     public void onTick(long millisUntilFinished) {

     }

     public void onFinish() {
         //retry
         //restart with timer.start();
     }
  }.start();

但是有很多选择:AsyncTask、Threads、Runnable。

例如:

重复一个有时间延迟的任务?

于 2013-05-31T12:31:02.583 回答