2014-01-24 37 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