2

I want to execute this instruction after certain milliseconds.

How can i achieve this?

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);
} 
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
Uday
  • 1,619
  • 3
  • 23
  • 48

3 Answers3

2

I would use a Handler to post a Runnable, like so:

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

...

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

The code will execute 100 milliseconds after the postDelayed call.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
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);
AnilPatel
  • 2,356
  • 1
  • 24
  • 40
1

I usually use 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();

But there are a lot of alternatives: AsyncTask, Threads, Runnable.

For example:

Repeat a task with a time delay?

Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129