Async

2021. 5. 30. 23:11

Async ๋ž€ ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ

thread๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ž‘์—…์„ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

Async ์žฅ์ 

Main Thread๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋„คํŠธ์›Œํฌ ์ž‘์—…์— ์œ ์šฉํ•˜๋‹ค.

 

Async ๋‹จ์ 

์žฌ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ตฌํ˜„๋œ activity๊ฐ€ ์ข…๋ฃŒ๋  ๊ฒฝ์šฐ ๋”ฐ๋ผ์„œ ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค.

AsyncTask๋Š” ํ•˜๋‚˜๋งŒ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. (๋ณ‘๋ ฌ์ฒ˜๋ฆฌ ๋ถˆ๊ฐ€)

 

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ Async ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•

AsyncTask๋ฅผ ์ƒ์† ๋ฐ›๋Š”๋‹ค

  • onPreExcute : thread ์ถœ๋ฐœํ•˜๊ธฐ ์ „ ํ•  ์ž‘์—…
  • doInBackground : thread๊ฐ€ ํ•  ์ž‘์—…
  • onProgressUpdate : ์ค‘๊ฐ„์ค‘๊ฐ„์— MainThread๋กœ ๋ณด๊ณ 
  • onPostExcute : ์ž‘์—…์„ ๋‹ค ๋งˆ์นœ ํ›„ MainThread๋กœ ๋ณต๊ท€

 

Layout ๊ตฌํ˜„

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".AsyncActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/ment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AsyncTask"
        android:textSize="40dp"/>

    <androidx.core.widget.ContentLoadingProgressBar
        android:id="@+id/progrssbar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:max="100"/>

    <Button
        android:id="@+id/start"
        android:layout_marginTop="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์‹œ์ž‘"/>

    <Button
        android:id="@+id/stop"
        android:layout_marginTop="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="์ทจ์†Œ"/>




</LinearLayout>

 

Activity ๊ตฌํ˜„

package com.example.prac_android

import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.ProgressBar
import android.widget.TextView
import com.example.prac_android.databinding.ActivityAsyncBinding
import java.lang.Exception

class AsyncActivity : AppCompatActivity() {
    private lateinit var binding: ActivityAsyncBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAsyncBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var task : BackgroundAsyncTask? = null
        binding.start.setOnClickListener {
            task = BackgroundAsyncTask(binding.progrssbar, binding.ment)
            task?.execute()
        }
        binding.stop.setOnClickListener{
            task?.cancel(true)
        }

    }
}

class BackgroundAsyncTask(
    val progressbar : ProgressBar,
    val progressText : TextView
) : AsyncTask<Int, Int, Int>(){
    //params : doInBackground ์—์„œ ์‚ฌ์šฉํ•  ํƒ€์ž… ๊ฒฐ์ •
    //progress : onProgressUpdate ์—์„œ ์‚ฌ์šฉํ•  ํƒ€์ž… ๊ฒฐ์ •
    //result : onPostExecute ์—์„œ ์‚ฌ์šฉํ•  ํƒ€์ž… ๊ฒฐ์ •
    var percent:Int = 0

    //์‹คํ–‰ํ•˜๊ธฐ ์ง์ „์ „
   override fun onPreExecute() {
        percent = 0
        progressbar.progress = percent
    }
    override fun doInBackground(vararg params: Int?): Int {
        while (!isCancelled){
            percent++
            if(percent > 100){
                break
            } else {
                publishProgress(percent)
            }

            //๊ณผ์ •์„ ์ž˜ ๋ณด๊ธฐ ์œ„ํ•ด ์‹œ๊ฐ„ ์ง€์—ฐ
            try {
                Thread.sleep(100)
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
        return percent
    }

    override fun onProgressUpdate(vararg values: Int?) {
        progressbar.progress = values[0] ?: 0
        progressText.text = "ํผ์„ผํŠธ : " +  values[0]
        super.onProgressUpdate(*values)

    }

    // doInBackground๊ฐ€ ๋๋‚ฌ์„ ๋•Œ returnํ•œ ํƒ€์ž…์ด ๋“ค์–ด์˜ด
    override fun onPostExecute(result: Int?) {
        progressText.text = "์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
    }

    //์ทจ์†Œ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ
    override fun onCancelled() {
        progressbar.progress = 0
        progressText.text = "์ž‘์—…์ด ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
    }
}
728x90

BELATED ARTICLES

more