2013-08-08 6 views
0

나는 android에서 새롭다. 당신은 우리가 볼 수 있듯이UI 스레드 및 Surfaceview

class LunarView extends SurfaceView implements SurfaceHolder.Callback { 

//... 

public LunarView(Context context, AttributeSet attrs) { 

     class LunarThread extends Thread { 

     //... 

     private void doDraw(Canvas canvas) { 
       //do some drawing on the canvas 
     } 
     } 

     //... 

    public LunarView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     // ... 

     // create thread only; it's started in surfaceCreated() 
     thread = new LunarThread(holder, context, new Handler() { 
      @Override 
      public void handleMessage(Message m) { 
       mStatusText.setVisibility(m.getData().getInt("viz")); 
       mStatusText.setText(m.getData().getString("text")); 
      } 
     }); 

     //... 

    } 


    //... 

    } 

: 당신이 안드로이드 SDK의 LunarLander 샘플을 보면,이 코드를 볼 수 있습니다

1 : Do not block the UI thread 
2 : Do not access the Android UI toolkit from outside the UI thread 

: 나는 안드로이드 개발자 웹 사이트에서 안드로이드 스레딩에 대한 두 가지 규칙을 보았다 뷰를 렌더링하고있는 새로운 thread를 작성합니다.

이 코드가 안드로이드 프레임 워크의 스레딩 2 규칙을 위반하지 않는 이유는 무엇입니까?

읽어 주셔서 감사합니다.

답변

0

이 코드는 이전에 시도한 적이 없었습니다. 하지만 handleMessage(Message m) 메인 (UI) 스레드에서 실행되는 것 같아요. 이를 확인하기 위해 검사 할 코드를 추가 할 수 있습니다.

thread = new LunarThread(holder, context, new Handler() { 
      @Override 
      public void handleMessage(Message m) { 
       if (Thread.currentThread().getId() == 1) { 
        // main thread 
       } 
       else { 
        // other thread 
       } 
       mStatusText.setVisibility(m.getData().getInt("viz")); 
       mStatusText.setText(m.getData().getString("text")); 
      } 
     }); 
0

스레드가보기를 렌더링하지 않습니다. 어떻게 작동하는지 완전히 모르겠지만 핸들러를 사용하여 뷰를 소유 한 스레드에 메시지를 보냅니다. LunarLander는이 예제와 같은 방법으로 스레드를 관리합니다. https://developer.android.com/training/multiple-threads/communicate-ui.html

+0

네, 링크 해 주셔서 감사합니다. –

+0

다른 스레드에서 렌더링 할 수 있습니다. 이것은 예제의 핵심입니다 : * 나중에 * MainUI로 전송 된 캔버스에 쓰는 두 번째 스레드를 생성하십시오. – RichieHH