2014-01-24 35 views
1

在我的onResume()我有這樣的事情:的Android runOnUiThread調用的onResume()再次

@Override 
    protected void onResume() {  
    super.onResume(); 
      abc(); 
    } 

,並在美國廣播公司()我有:

new Thread(new Runnable() {    
    public void run() { 
     MainActivity.activity.runOnUiThread(new Runnable() { 

    public void run() { 
    //some code 
    } 
    }); 
    } 
}).start(); 
//do something 

但是似乎runOnUiThread通過調用的onResume runOnUiThread,因爲我注意到/ /做的事情是做了兩次...

我不知道是否有工作?基本上我需要abc()等待10秒鐘,然後在屏幕上的文本框中顯示一條消息。

回答

0
I need abc() to wait 10 seconds and then display a message in a textfield on the screen. 

您可以使用Handler。在abc()

Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       // do something 
      } 
     }, 10000); 

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

public final boolean postDelayed (Runnable r, long delayMillis) 

Added in API level 1 
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. 

Parameters 
r The Runnable that will be executed. 
delayMillis The delay (in milliseconds) until the Runnable will be executed. 
0

您可以在ABC添加以下代碼代碼()。

runOnUiThread(new Runnable() { 

    public void run() { 
     try { 
      Thread.sleep(10000); 
     } catch (InterruptedException e) { 
     e.printStackTrace(); 
     } 
     //do your task 
    } 
}); 
+0

睡在一個UI線程上。你阻止了ui線程。錯誤。你不應該阻止ui線程。它在文檔中提到,如果你想檢查 – Raghunandan