2011-08-18 7 views
1

아무에게 말해 줄 수 있습니까? 메시지 대기열에서 데이터 (메시지)를 가져 오는 방법은 무엇입니까? 또는 어떻게 메인 스레드에서 다른 스레드로 메시지를 보낼 수 있습니까?안드로이드의 메시지 대기열

감사

답변

0

되지 않은 다른 스레드 ... 할 수 있습니다 센 그것을 주 스레드 또는 UI 스레드, Handler를 사용하여 ..

9

당신이 만약 .. 핸들러를 작성하고 anarguement으로 실행 가능한 개체를 보내 스레드에서 메시지를 받으려면 Looper을 실행하고 Handler 메시지를이 루퍼에 바인딩하십시오. UI 스레드에는 기본적으로 루퍼가 있습니다. HandlerThread이라는 루퍼로 스레드를 생성하기위한 편리한 클래스가 있습니다. 처리기 및 루퍼에 대한 유용한 문서는 Android Guts: Intro to Loopers and Handlers입니다.

편집 :

HandlerThread thread = new HandlerThread("Thread name"); 
thread.start(); 

Looper looper = thread.getLooper(); 
Handler handler = new Handler(looper) { 
    @Override 
    public void handleMessage(Message msg) { 
     switch(msg.what) { 
      case SOME_MESSAGE_ID: 
       // SOME_MESSAGE_ID is any int value 
       // do something 
       break; 
      // other cases 
     } 
    } 
}; 

handler.post(new Runnable() { 
    @Override 
    public void run() { 
     // this code will be executed on the created thread 
    } 
}); 

// Handler.handleMessage() will be executed on the created thread 
// after the previous Runnable is finished 
handler.sendEmptyMessage(SOME_MESSAGE_ID); 
+0

나는 오디오 기록을 원하고 메시지 큐를 사용하여 재생 ... – user900591

+0

는 u는 나에게 이것에 대한 적절한 예를 들어 줄 수 있습니까? 감사합니다 ...... – user900591

+0

예를 추가했습니다. – Michael

관련 문제