날짜와 시간 실행을위한 스레드를 볼 수 있습니다. 그러나 멈추는 시계를위한 아무 실도 없다. 무엇이 문제 일 수 있겠습니까?스톱워치가 작동하지 않습니다.
공용 클래스 MainActivity는 활동 내가 날짜와 시간이 실행 스레드를 볼 수 있습니다
{
private Button Start, Reset, Stop;
private EditText stop_watch, lblDate, lblTime;
public MainActivity() {
}
private final UpdateClockThread ucThread = new UpdateClockThread();
private final StopwatchThread swThread = new StopwatchThread();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Start = (Button)findViewById(R.id.button1);
Stop = (Button)findViewById(R.id.button3);
Reset = (Button)findViewById(R.id.button2);
stop_watch = (EditText)findViewById(R.id.editText3);
lblTime = (EditText)findViewById(R.id.editText2);
lblDate = (EditText)findViewById(R.id.editText1);
init();
Start.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startactionPerformed();
}
});
Stop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopactionPerformed();
}
});
Reset.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
resetactionPerformed();
}
});
}
public void init() {
swThread.setDaemon(true);
ucThread.setDaemon(true);
swThread.start(); // this doesent seem to wrk..
ucThread.start();
}
/** Listens to the Start/Stop/Resume button. */
void startactionPerformed() {
swThread.go();
}
void stopactionPerformed() {
swThread.noGo();
}
void resetactionPerformed() {
swThread.reset();
}
/** A thread that updates the current date & time. */
private class UpdateClockThread extends Thread {
/** The actual work of the thread. */
public void run() {
while (true) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Calendar now = Calendar.getInstance();
String month = Integer.toString(now.get(Calendar.MONTH)+1);
String date = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
String year = Integer.toString(now.get(Calendar.YEAR));
String hour = Integer.toString(now.get(Calendar.HOUR));
if (hour.equals("0")) hour = "12";
String minute = Integer.toString(now.get(Calendar.MINUTE));
if (minute.length() == 1) minute = "0" + minute;
String second = Integer.toString(now.get(Calendar.SECOND));
if (second.length() == 1) second = "0" + second;
String ampm = now.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
lblDate.setText(month + "/" + date + "/" + year);
lblTime.setText(hour + ":" + minute + ":" + second + " " + ampm);
}
});
try {
sleep(500);
} catch (InterruptedException e) {}
}
}
}
/** A thread that keeps track of the stop watch & updates
* the display accordingly.
*/
class StopwatchThread extends Thread {
/** Whether or not stop watch is running. */
private boolean going = false;
/** Stores elapsed milliseconds of previous runs. */
private long prevElapsed = 0;
/** Stores beginning time of this run. */
private Date startDate = new Date();
/** Returns elapsed time in milliseconds.
*@return The elapsed time
*/
private long elapsedTime() {
return prevElapsed + (going ? new Date().getTime() - startDate.getTime() : 0);
}
/** Changes the number of elapsed milliseconds into a string.
*@param time Number of elapsed milliseconds
*@return The elapsed time as a string.
*/
private String msToString(long time) {
String ms, sec, min;
if (time % 10 >= 5) //round to nearest hundredth
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() < 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
min = Long.toString(time);
stop_watch.setText(min);
return min + ":" + sec + "." + ms;
}
/** Called when the stop watch is to go.
*/
public void go() {
startDate = new Date();
going = true;
}
/** Called when the stop watch is to stop.
*/
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
/** Resets the stop watch.
*/
public void reset() {
going = false;
prevElapsed = 0;
}
/** Adds a lap to the list.
*/
/** Main code of the thread.
*/
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
stop_watch.setText(msToString(elapsedTime()));
}
});
yield();
}
}
}을 확장합니다. 그러나 멈추는 시계를위한 아무 실도 없다. 무엇이 문제 일 수 있겠습니까? 내가 활성 스레드를 종료 할 경우에도
어떻게 ..
내가 내 프로그램이 개 데몬 스레드를 가지고 .. 스톱워치 스레드가 .. 도와주세요 시작 ..하지만 하나가 활성화 쇼를 기록 할 수 있습니다. 스톱워치는 아직 깨지지 않습니다. 나는 많은 것을 시도했다. 그러나 .. 문제 어디인지 알아낼 수없는
DalvikVM 임 [로컬 호스트 : 8633]
스레드 [< 1> 주 (실행)
스레드 [< 8> 바인더 스레드 # 2] (실행)
스레드 [< 7> 바인더 스레드 1 (실행)
데몬 스레드 [< 10> 스레드 (10) (실행)
보다 구체적인 방법으로 문제를 설명해야합니다. 코드 묶음을 넣으면 다른 사람이 그것을 고쳐 줄 것을 기대하지 마십시오 : P –
좀 더 자세하게 설명하려고했습니다 .. 더 많은 것을 공유 할 수있는 것은 없습니다 .. 모든 것이 훌륭합니다. 두 개의 스레드 .. 몰라 왜 몰라 .. 그리고 만약 그 스레드를 다시 시작하려고 메신저 강제로 가까운 문제가 점점. –