我在游戏中工作所以我需要使用每秒更新的时间。我正在使用TimerTask.I想要暂停时间当我点击按钮并希望在我点击恢复按钮时再次恢复时来自其他活动。如何做,请帮助我。
t=new Timer();
{
t.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
TextView tv = (TextView) findViewById(R.id.time);
tv.setText(String.format("%02d:%02d",minute,seconds));
time += 1;
seconds += 1;
if(seconds==60)
{
seconds=0;
}
minute=time/60;
}
});
}
}, 0, 1000);
}
答案 0 :(得分:0)
首先,使用CountDownTimer会更好。 CountDownTimer在内部使用Handler并在UI(主线程)上执行回调。这样可以使代码更清晰。
CountDownTimer timer = new CountDownTimer(1000000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
timer.start();
现在您需要将一个Activity的引用传递给另一个Activity。我认为最常见,最简单和最丑陋的方法是使用静态解析器类。
public class CountDownTimer {
private static Alarms sCountDownTimer;
public static CountDownTimer getCountDownTimer () {
if (sCountDownTimer == null) throw new NullPointerException("CountDownTimer not initialized yet");
return sCountDownTimer;
}
public static void init(CountDownTimer countDownTimer) {
if (sCountDownTimer == null) {
sCountDownTimer = countDownTimer;
} else throw new RuntimeException("Attept to reinitialize!");
}
}
答案 1 :(得分:0)
Try this this is what You want
just copy and paste
public class MainActivity extends Activity {
EditText t;
Button b;
Button b2;
Timer timer;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
t=(EditText)findViewById(R.id.editText1);
b.setBackgroundResource(android.R.color.holo_green_dark);
timer = new Timer();
TimerTask updateProfile = new CustomTimerTask(MainActivity.this);
timer.scheduleAtFixedRate(updateProfile, 0,1000);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timer = new Timer();
TimerTask updateProfile = new CustomTimerTask(MainActivity.this);
timer.scheduleAtFixedRate(updateProfile, 0,1000);
}
});
}
class CustomTimerTask extends TimerTask {
private Context context;
private Handler mHandler = new Handler();
// Write Custom Constructor to pass Context
public CustomTimerTask(Context con) {
this.context = con;
}
@Override
public void run() {
new Thread(new Runnable() {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
Calendar c =Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int min= c.get(Calendar.MINUTE);
int hour= c.get(Calendar.HOUR);
t.setText(hour+":"+min+":"+seconds);
}
});
}
}).start();
}
}
}
答案 2 :(得分:0)
实际上,你更好地将内容发布到UI线程,延迟了。你想要这样的东西:
final TextView tv = (TextView) findViewById(R.id.time);
getMainLooper().postDelayed(
new Runnable() {
@Override void run() {
tv.setText(String.format("%02d:%02d",minute,seconds));
} },
60 * 1000)