티스토리 뷰
UI 작업은 Main Thread에서 해야 하는데 만약 Worker Thread에서 UI 변경을 해야 한다면 Handler를 이용하여 Main Thread와 연결해 주어야 한다. 단, Handler는 Main Thread에서 생성이 되어야 한다.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ready"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/btnStart"
android:layout_width="180dp"
android:layout_height="80dp"
android:text="Start"
android:textSize="16dp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/tvText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btnStop"
android:layout_width="180dp"
android:layout_height="80dp"
android:text="Stop"
android:textSize="16dp"
app:layout_constraintTop_toBottomOf="@+id/btnStart"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.jwsoft.javaproject;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvText;
Button btnStart;
Button btnStop;
MyThread myThread;
MyHandler myHandler;
public final static int SEND_START = 0;
public final static int SEND_STOP = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHandler = new MyHandler();
tvText = findViewById(R.id.tvText);
btnStart = findViewById(R.id.btnStart);
btnStop = findViewById(R.id.btnStop);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myThread != null) {
myThread.stopThread();
}
myThread = new MyThread();
myThread.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myHandler.sendEmptyMessage(SEND_STOP);
}
});
}
class MyThread extends Thread {
int i = 0;
Boolean stopped = false;
public void stopThread() {
stopped = true;
}
@Override
public void run() {
super.run();
while (!stopped) {
Message message = Message.obtain();
message.what = 0;
message.arg1 = i++;
message.obj = " 초 경과";
myHandler.sendMessage(message);
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyHandler extends Handler {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SEND_START:
tvText.setText(Integer.toString(msg.arg1) + msg.obj);
break;
case SEND_STOP:
tvText.setText("Stop");
myThread.stopThread();
break;
default:
break;
}
}
}
}
참고.
'Android > Java' 카테고리의 다른 글
[Java] Retrofit2 (0) | 2020.06.27 |
---|---|
[Java] Handler.post (0) | 2020.06.27 |
[Java] RecyclerView (0) | 2020.06.27 |
[Java] Button 활용 (0) | 2020.06.11 |
[Java] TextView 접근 (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Android
- Design Pattern
- Architecture Pattern
- Intent
- 안드로이드 #코틀린 #Android #Kotlin
- 안드로이드
- Vue.js #Vue.js + javascript
- James Kim
- JSONArray
- recyclerview
- 자바
- 혀가 길지 않은 개발자
- JSONObject
- ViewPager2
- TabLayout
- ArrayList
- Livedata
- CoordinatorLayout
- fragment
- activity
- java
- XML
- coroutine
- handler
- DataBinding
- Kotlin
- MVVM
- ViewModel
- 코틀린
- View
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함