在我的onResume()中我有这样的东西:
@Override
protected void onResume() {
super.onResume();
abc();
}
在abc()中我有:
new Thread(new Runnable() {
public void run() {
MainActivity.activity.runOnUiThread(new Runnable() {
public void run() {
//some code
}
});
}
}).start();
//do something
然而似乎runOnUiThread通过runOnUiThread调用onResume,因为我注意到//做了两次事情......
我想知道是否有解决方法?基本上我需要abc()等待10秒,然后在屏幕上的文本字段中显示一条消息。
答案 0 :(得分: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.
答案 1 :(得分:0)
您可以在abc()中的代码下面添加代码。
runOnUiThread(new Runnable() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//do your task
}
});