프로그래밍/안드로이드

안드로이드 쓰레드 3번째 쓰레드를 구현하는 클래스와 메인 클래스를 따로 만들어서 처리해보자

가카리 2012. 8. 21. 21:15
반응형

쓰레드 3번째 쓰레드를 구현하는 클래스와 메인 클래스를 따로 만들어서 처리해보자

 

소스를 보면 아 이렇게 구현해도 되는구나 이런 느낌이 들겁니다.

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

> 

<TextView

android:id="@+id/mainvalue"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20sp"

android:text="MainValue : 0"

/>

<TextView

android:id="@+id/backvalue"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20sp"

android:text="BackValue : 0"

/>

<Button

android:id="@+id/increase"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="mOnClick"

android:text="증가"

/>

</LinearLayout>

 

 

 

자바파일

 

package com.android.ex92;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.TextView;

 

public class ex92 extends Activity {

int mMainValue=0;

TextView mMainText;

TextView mBackText;

BackThread mThread;

      

       /** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

 

mMainText = (TextView)findViewById(R.id.mainvalue);

mBackText = (TextView)findViewById(R.id.backvalue);

mThread = new BackThread(mHandler);

mThread.setDaemon(true);

mThread.start();

 

}

 

public void mOnClick(View v){

       mMainValue++;

       mMainText.setText("MainValue : " + mMainValue);

}

 

Handler mHandler = new Handler(){

 

             @Override

             public void handleMessage(Message msg) {//쓰레드 처리하는 부분

                    // TODO Auto-generated method stub

                    if(msg.what == 0){//확인해보고

                           mBackText.setText("BackValue : " + msg.arg1);

                           //메세지객체의 멤버변수를 이용해서 텍스트뷰를 바꿔줌

                    }

             }

      

};

}

 

//쓰레드를 구현하는 클래스를 다른 자바파일에 만든다고 가정

class BackThread extends Thread{

       int mBackValue = 0;

       Handler mHandler;

      

       BackThread(Handler mHandler){//핸들러를 생성자로 이용해서 받아옴

             this.mHandler = mHandler;

       }

      

       @Override

       public void run() {

             // TODO Auto-generated method stub

             while(true){

                    mBackValue++;

                    Message msg = new Message();//메세지 객체를 하나 만들고

                    msg.what = 0;//정보를 넣어줌

                    msg.arg1 = mBackValue;

                    mHandler.sendMessage(msg);//그리고 sendMessage 쓰레드 호출

                    try{

                           Thread.sleep(1000);//1초마다 계속 하게됨

                    }catch(InterruptedException e){;}

             }

            

            

       }

      

      

}

 

 

 

 

반응형