2

I am quite noob I have a cronometer that begins to counter when I click a button

    crono = (Chronometer) findViewById(R.id.crono);
begin= (Button) findViewById(R.id.begin);
begin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {

        crono.setBase(SystemClock.elapsedRealtime());
        crono.start();

every minute do something ////////

    }
});

What should I do to do something every minute? For example 1:00 ---> do something 2:00 ---> do something

Any suggestion will be apreciated. :)

4

1 回答 1

1

尝试使用OnChronometerTickListener

@Override
public void onClick(View arg0) {
    crono.setBase(SystemClock.elapsedRealtime());
    crono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        private long mTicks = 0;
        public void onChronometerTick(Chronometer chronometer) {
            if ((mTicks / 60) == 0) { // adapt period here
                // do something
            }
            mTicks++;
        }
    });
    crono.start();
}

我不确定,Cronometer 滴答作响的频率,我的猜测是每秒。你可能需要调整这个时期,如果它是别的东西。

如果不是固定周期,则需要检查时间,例如:

@Override
public void onClick(View arg0) {
    crono.setBase(SystemClock.elapsedRealtime());
    crono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        private long mLastAction = 0;
        public void onChronometerTick(Chronometer chronometer) {
            now = System.getCurrentMillies();
            if (now - mLastAction > 60 * 1000) {
                // do something
                mLastAction = now;
            }
        }
    });
    crono.start();
}

也许,无论如何,第二个更好。

于 2013-09-06T11:11:59.790 回答