0

我正在玩游戏,所以我需要使用每秒更新的时间。我正在使用 TimerTask。我想暂停时间当我点击一个按钮并想在我点击其他活动的恢复按钮时再次恢复它.如何做请帮助我。

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);

        }
4

3 回答 3

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)
于 2013-03-24T14:58:06.697 回答
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();

        }

    }
}
于 2013-03-24T14:51:46.217 回答
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 传递到另一个。我认为最常见、最简单和最丑陋的方法是拥有一个静态解析器类。

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!");
    }
}
于 2013-03-24T14:23:11.253 回答