package com.navikolus.timerExample;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Chronometer;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChronometer = (Chronometer) findViewById(R.id.chronometer);
mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
long elapsedMillis = SystemClock.elapsedRealtime()
- mChronometer.getBase();
if (elapsedMillis > 5000) {
String strElapsedMillis = "Прошло больше 5 секунд";
Toast.makeText(getApplicationContext(),
strElapsedMillis, Toast.LENGTH_SHORT)
.show();
}
}
});
}
public void onStartClick(View view) {
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
}
public void onStopClick(View view) {
mChronometer.stop();
}
public void onResetClick(View view) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
}