ANR

◎메인 스레드의 특징

5초 이상 작업을 하느라 화면이 멈추거나..

어플 종료(ANR - android not response)

CPU/ram Fast  -  Graphic은 자원을 많이 소모한다.

계산은 금방하고  - 화면 출력은 나중으로 미룸


◎서브스레드의 특징

계산은 잘한다 - Graphic 처리를 못함(android에서만)

        - View에 접근할 수 없다.


◎작업영역      ----......-----         뷰영역       

                          핸들러






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:textSize="30dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:textSize="30dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="시작" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="정지" />

</LinearLayout>



package com.gusfree.thread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ThreadActivity extends Activity {

	TextView tv1,tv2;//선언
	Button btn1, btn2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		//클래스 변수 초기화
		tv1=(TextView) this.findViewById(R.id.textView1);
		tv2=(TextView) this.findViewById(R.id.textView2);
		btn1=(Button) this.findViewById(R.id.button1);
		btn2=(Button) this.findViewById(R.id.button2);

		//버튼에 리스너 셋팅 callBack메서드 셋팅
		ButtonListener listener = new ButtonListener(); 
		btn1.setOnClickListener(listener);
		btn2.setOnClickListener(listener);
	}
	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.button1 :
				Plus plus1 = new Plus();//스레드 겍체 생성
				plus1.start();//run()안에 적어둔 일을 해라
				break;
			case R.id.button2 :
				Log.i("버튼 2클릭","버튼 2클릭");
				tv1.setText(0+"");
				break;
			}
		}
	}
	//innerClass 사용목적 :
	//OutterClass의 변수를 자율롭게 사용하기 위해
	class Plus extends Thread{

		//변수선언 및 초기화
		MyHandler handler = new MyHandler();

		@Override
		public void run() {//스레드가 할 일을 적어두는 곳
			android.util.Log.i("버튼 1클릭","버튼 1클릭");//import가 필요없다.
			/*1~100 숫자를 textview에 찍자*/
			for(int i=0;i<=100;i++){

				//서브스레드는 View에 접근할 수 없다.
				//tv1.setText(i+"");//Error

				//대안:핸들러에게 View에 접근하는일 시키자
				//send~~Message  : handleMessage호출
				Log.i("sendEmptyMessage","sendEmptyMessage"+i);
				handler.sendEmptyMessage(i);

				try{
					Thread.sleep(50);//0.05초 *100=5
				}catch(InterruptedException e){
					e.printStackTrace();
				}
			}
		}
	}
	//inner Class:서브스레드로부터 지시를 받아서 뷰 수정
	class MyHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			Log.e("handleMessage","handleMessage");
			int number=msg.what;
			tv1.setText(number+"핸들러가 찍습니다.");
			
			super.handleMessage(msg);
		}
	}
} 



'Android > 2012.04월 강좌' 카테고리의 다른 글

7일차 Draw  (0) 2012.05.04
7일차 Thread 2  (0) 2012.05.04
7일차 Notification  (0) 2012.05.04
7일차 AnimationSet  (0) 2012.05.04
7일차 ScaleAnimation  (0) 2012.05.04

+ Recent posts