我需要在我的应用程序中包含计时器。我刚刚浏览了sites.and中给出的示例。并尝试在我的程序中实现它。但事情是我不知道如何使用计时器。我想要的程序在按钮上启动计时器click.timer必须在屏幕上显示。但是屏幕是一个画布(当我们点击它加载画布的按钮时)带有移动的物体。当两个物体碰撞时,计时器必须停止并且必须显示消息框中的值。以下是我的主要活动代码。
public class MyActivity extends Activity {
private static final String TAG = MyActivity.class.getSimpleName();
public static Timer myTimer;
private Button startbtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startbtn=(Button)findViewById(R.id.startbtn);
startbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
setContentView(new MainGamePanel(getApplicationContext()));
Log.d(TAG, "View added");
}
});
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}
@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
}
我知道如何检查collision.i只需要计时器部分,如启动计时器,在屏幕上显示并从另一个类停止它。任何人都可以帮助我...... PLZ ....
答案 0 :(得分:1)
使用此代码将在一秒钟后重复,计时器将运行30秒
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
}
}.start();