public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView mTextView;
private Button mButton,mButton1;
private CountDownTimer mTimer;
private long current=30000;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"onCreate");
initView();
}
private void initView() {
mTextView = (TextView) findViewById(R.id.mTextView);
mButton = (Button) findViewById(R.id.mButton);
mButton1 = (Button) findViewById(R.id.mButton1);
mTextView.setOnClickListener(this);
mButton.setOnClickListener(this);
mButton1.setOnClickListener(this);
CreateTimer(0);
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"onResume");
CreateTimer(1);
mTimer.start();
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"onPause");
mTimer.cancel();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.mTextView:
break;
case R.id.mButton:
mTimer.cancel();
break;
case R.id.mButton1:
CreateTimer(1);
mTimer.start();
break;
}
}
private void CreateTimer(long count){
if(count==0){
current = 30000;
}
mTimer = new CountDownTimer(current, 1000){
@Override
public void onTick(long l) {
current = l;
mTextView.setText("this is "+l/1000);
}
@Override
public void onFinish() {
mTextView.setText("done");
current = 30000;
}
};
}
}