Today i get a open source for timer. But this code has a little problem.
When I push the stop button over running this handler of app , it is well working to push start button.
But when I push start button and push pause button and push stop button, I have to start button twice.
I tried to fix this problem. But it was all just a big waste of time! T^T
The code is here.. plz help me
public class StopWatchActivity extends AppCompatActivity {
private Button mStartBtn, mStopBtn, mRecordBtn, mPauseBtn;
private TextView mTimeTextView, mRecordTextView;
private Thread timeThread = null;
private Boolean isRunning = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#4ea1d3"));
}
mStartBtn = (Button) findViewById(R.id.btn_start);
mStopBtn = (Button) findViewById(R.id.btn_stop);
mRecordBtn = (Button) findViewById(R.id.btn_record);
mPauseBtn = (Button) findViewById(R.id.btn_pause);
mTimeTextView = (TextView) findViewById(R.id.timeView);
mRecordTextView = (TextView) findViewById(R.id.recordView);
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
mStopBtn.setVisibility(View.VISIBLE);
mRecordBtn.setVisibility(View.VISIBLE);
mPauseBtn.setVisibility(View.VISIBLE);
timeThread = new Thread(new timeThread());
timeThread.start();
}
});
mStopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
mRecordBtn.setVisibility(View.GONE);
mStartBtn.setVisibility(View.VISIBLE);
mPauseBtn.setVisibility(View.GONE);
mRecordTextView.setText("");
mTimeTextView.setText("00:00:00:00");
timeThread.interrupt();
}
});
mRecordBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecordTextView.setText(mRecordTextView.getText() + mTimeTextView.getText().toString() + "\n");
}// 앞에 mRecordTextView.getText()은 n번이상 저장할때 첫번째 값을 n-1번째 라인에 놓고
}); // n번째 저장한것을 n번째 놓기 위해서 설정
mPauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isRunning = !isRunning;
if (isRunning) {
mPauseBtn.setText("PAUSE");
} else {
mPauseBtn.setText("PAUSE");
}
}
});
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int mSec = msg.arg1 % 100;
int sec = (msg.arg1 / 100) % 60;
int min = (msg.arg1 / 100) / 60;
int hour = (msg.arg1 / 100) / 360;
//1000이 1초 1000*60 은 1분 1000*60*10은 10분 1000*60*60은 한시간
@SuppressLint("DefaultLocale")
String result = String.format("%02d:%02d:%02d:%02d", hour, min, sec, mSec);
mTimeTextView.setText(result);
}
};
public class timeThread implements Runnable {
@Override
public void run() {
int i = 0;
while (true) {
while (isRunning) { //일시정지를 누르면 멈춤
Message msg = new Message();
msg.arg1 = i++;
handler.sendMessage(msg);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
runOnUiThread(new Runnable(){
@Override
public void run() {
mTimeTextView.setText("");
mTimeTextView.setText("00:00:00:00");
}
});
return; // 인터럽트 받을 경우 return
}
}
}
}
}
}
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
mStopBtn.setVisibility(View.VISIBLE);
mRecordBtn.setVisibility(View.VISIBLE);
mPauseBtn.setVisibility(View.VISIBLE);
timeThread = new Thread(new timeThread());
if(isRunning !=true){
isRunning = true; /This code is a solution that I think.
}
timeThread.start();
Log.v("here is start ","stat!");
}
});
I got the solution. I upload the code to help for someone who have same promble like me