Roh*_*ish 3 java android timer
怎么能在android中完成?
public final Timer timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do task here
}
});
Run Code Online (Sandbox Code Playgroud)
我需要能够调用timer.start(); 来自计时器所在的活动.
在大多数情况下,使用Handler而不是Timer更好.处理程序能够发送延迟消息.使用Handler的好处是:
例:
class MyActivity extends Activity {
private static final int DISPLAY_DATA = 1;
// this handler will receive a delayed message
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Do task here
if (msg.what == DISPLAY_DATA) displayData();
}
};
@Override
void onCreate(Bundle b) {
//this will post a message to the mHandler, which mHandler will get
//after 5 seconds
mHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 5000);
}
}
Run Code Online (Sandbox Code Playgroud)