안드로이드

 

 

 

 

class MainActivity

package org.androidtown.myasynctask;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    ProgressBar progressBar;
    int value=0;
    BackGroundTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView=(TextView)findViewById(R.id.txt1);
        progressBar=(ProgressBar)findViewById(R.id.progressBar);
    }


    public void onClick(View v){

        switch (v.getId()){

            case R.id.button:
                task =new BackGroundTask();
                task.execute(100);
                Toast.makeText(getApplicationContext(), "시작" , Toast.LENGTH_SHORT).show();
                break;

            case R.id.button2:

                task.cancel(true);
                Toast.makeText(getApplicationContext(), "중지" , Toast.LENGTH_SHORT).show();
                break;
        }

    }



    class BackGroundTask extends AsyncTask<Integer, Integer, Integer>{

        public BackGroundTask() {
        }

        @Override
        protected void onPreExecute() {
            value=0;
            progressBar.setProgress(0);
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            while (!isCancelled()){
                value++;
                if(value >=100){
                    break;
                }else {
                    publishProgress(value);
                }

                try {
                    Thread.sleep(200);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
            return value;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            progressBar.setProgress(values[0].intValue());
            textView.setText("진행중 :" + String.valueOf(values[0].intValue()));
        }

        @Override
        protected void onPostExecute(Integer integer) {
            value=0;
            progressBar.setProgress(0);
            textView.setText("중지됨");
        }

    }






}

 

 

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.androidtown.myasynctask.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="진행상태"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/txt1"
        android:textAlignment="center" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:id="@+id/progressBar"
        android:max="100"
        />

    <Button
        android:text="시작"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:id="@+id/button"
        android:onClick="onClick"
        />

    <Button
        android:text="중지"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button"
        android:layout_marginTop="47dp"
        android:id="@+id/button2"
        android:onClick="onClick"

        />



</RelativeLayout>

 

 

 

 

 

85강

 

 

86강

 

 

 

about author

PHRASE

Level 60  머나먼나라

진정한 개인의 경제적 보장과 독립없이 존재하지 않는다. 굶주리고 직업이 없는 국민은 독재가 행해질 그럴 듯한 이유가 된다. - F.D. 루즈벨트

댓글 ( 4)

댓글 남기기

작성