【发布时间】:2011-06-19 22:42:53
【问题描述】:
即使活动暂停,我也想在计时器计时时将活动放在前面。
非常感谢..
我的代码如下:
package cem.examples.wsAct;
import something....
public class main extends Activity {
TextView tvResult, tvCount;
Button btn;
Timer timer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// setviews ....
// (find on the layout and bind them to the fields)
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// bring activity to front
f_UpdateUI();
}
});
}
}, 1000, 3000);
}
void f_UpdateUI() {
String result = f_RetrieveFromWebService();
// ??? Code... ???
// If the activity is sended to back (how can I get it's state?)
// Bring the activity even if it is paused or stopped (here is the lost part)
}
private String f_RetrieveFromWebService() {
// connect to web service and return string
return "ta ta taaaaa";
}
}
【问题讨论】:
-
我认为这是不可能的,而且有充分的理由,因为某些活动在不期望的情况下不断弹出会很烦人。
-
@Noel 这可能很烦人,但也有可能:-)
标签: android android-activity timer