티스토리 뷰
Handler.post() vs runOnUiThread() vs AsyncTask.execute()
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="34dp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/btnHandler"
android:layout_width="200dp"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="Handler.post()"
android:textSize="18dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btnRunOnUiThread"
android:layout_width="200dp"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="runOnUiThread()"
android:textSize="18dp"
app:layout_constraintTop_toBottomOf="@+id/btnHandler"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btnAsyncTask"
android:layout_width="200dp"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="AsyncTast.execute()"
android:textSize="18dp"
app:layout_constraintTop_toBottomOf="@+id/btnRunOnUiThread"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvText;
Button btnHandler;
Button btnRunOnUiThread;
Button btnAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvText = findViewById(R.id.tvText);
btnHandler = findViewById(R.id.btnHandler);
btnRunOnUiThread = findViewById(R.id.btnRunOnUiThread);
btnAsyncTask = findViewById(R.id.btnAsyncTask);
final Handler handler = new Handler();
btnHandler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.post(new Runnable() {
@Override
public void run() {
tvText.setText("Handler.post() 클릭");
}
});
}
});
btnRunOnUiThread.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvText.setText("runOnUiThread() 클릭");
}
});
}
});
btnAsyncTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
tvText.setText("AsyncTask.execute() 클릭");
}
});
}
});
}
}
AsyncTask.execute() 버튼 클릭 후 에러 발생!
AsyncTask.execute()는 UI 작업을 할 수 없다.
AsyncTask는 기본으로 doInBackground()에서 돌아가기 때문이다.
'Android > Java' 카테고리의 다른 글
[Java] BottomSheetDialog (0) | 2020.07.01 |
---|---|
[Java] SharedPreferences + Singleton Pattern (0) | 2020.06.30 |
[Java] AsyncTask (0) | 2020.06.29 |
[Java] runOnUiThread (0) | 2020.06.28 |
[Java] Retrofit2 (0) | 2020.06.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ViewPager2
- java
- Android
- 혀가 길지 않은 개발자
- XML
- Livedata
- Vue.js #Vue.js + javascript
- ViewModel
- Design Pattern
- 안드로이드 #코틀린 #Android #Kotlin
- handler
- 자바
- JSONArray
- fragment
- Kotlin
- 코틀린
- coroutine
- TabLayout
- 안드로이드
- recyclerview
- Architecture Pattern
- JSONObject
- CoordinatorLayout
- DataBinding
- View
- MVVM
- ArrayList
- Intent
- activity
- James Kim
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함