티스토리 뷰
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:textAllCaps="false"
android:text="Start"
android:textSize="16dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MyAsyncTask.java
package com.jwsoft.javaproject;
import android.os.AsyncTask;
import android.widget.TextView;
public class MyAsyncTask extends AsyncTask<Void, Integer, Boolean> {
TextView tvText;
public MyAsyncTask(TextView tvText) {
this.tvText = tvText;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... voids) {
for (int i=1; i<=50; i++) {
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
publishProgress(i);
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
this.tvText.setText(Integer.toString(values[0]));
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
}
@Override
protected void onCancelled(Boolean aBoolean) {
super.onCancelled(aBoolean);
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvText;
Button btnStart;
MyAsyncTask myAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvText = findViewById(R.id.tvText);
btnStart = findViewById(R.id.btnStart);
myAsyncTask = new MyAsyncTask(tvText);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAsyncTask.execute();
}
});
}
}
한 번 더 클릭하면 에러 발생!
하나의 객체이므로 재사용 불가능.
execute() 한 번만 쓸 수 있음.
객체를 새로 생성해야 함.
(참고. Thread 객체도 start() 한 번만 사용 가능)
(그런 의미에서 Thread 객체랑 AsyncTask 객체랑 비슷하네)
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvText;
Button btnStart;
MyAsyncTask myAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvText = findViewById(R.id.tvText);
btnStart = findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAsyncTask = new MyAsyncTask(tvText);
myAsyncTask.execute();
}
});
}
}
'Android > Java' 카테고리의 다른 글
[Java] SharedPreferences + Singleton Pattern (0) | 2020.06.30 |
---|---|
[Java] Handler vs runOnUiThread vs AsyncTask (0) | 2020.06.29 |
[Java] runOnUiThread (0) | 2020.06.28 |
[Java] Retrofit2 (0) | 2020.06.27 |
[Java] Handler.post (0) | 2020.06.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JSONArray
- Kotlin
- James Kim
- java
- Vue.js #Vue.js + javascript
- Livedata
- Android
- fragment
- 안드로이드
- handler
- recyclerview
- coroutine
- Architecture Pattern
- CoordinatorLayout
- XML
- ViewPager2
- 혀가 길지 않은 개발자
- JSONObject
- 자바
- activity
- DataBinding
- ViewModel
- 코틀린
- MVVM
- ArrayList
- Design Pattern
- View
- 안드로이드 #코틀린 #Android #Kotlin
- TabLayout
- Intent
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함