IntentService 에 서 는 Toask 를 사용 하여 dead 스 레 드 에서 Handler 에 게 메 시 지 를 잘못 보 냅 니 다.

자신의 IntentSevice 계승 클래스 의 onHandleIntent 방법 에 Toast.makeText(getApplication Context(),"sd 는 존재 하지 않 습 니 다",Toast.LENGTHSHORT).show();logcat 에서"sending message to a Handler on a dead thread"오 류 를 알려 주지 않 습 니 다.이후 인터넷 에서 오류 가 발생 한 원인 을 통 해 한 라인 의 메시지 순환 이 이미 종 료 된 후에 더 이상 그 에 게 메 시 지 를 보 내지 못 하 는 것 이 오류 가 발생 하 는 것 보다 못 하 다 는 것 을 알 게 되 었 다.그 후에 한 사이트 에서 해결 방안 과 오류 가 발생 한 원인 을 찾 았 다.
The problem here is that you are creating a Toast inside a thread that is managed by the IntentService. The system will use the Handler associated with this thread to show and hide the Toast.
First the Toast will be shown correctly, but when the system tries to hide it, after the onHandleIntent method has finished, the error "sending message to a Handler on a dead thread" will be thrown because the thread on which the Toast was created is no longer valid, and the Toast will not disappear.
문 제 는 IntentService 가 관리 하 는 Toast 내부 스 레 드 를 만 들 고 있 습 니 다.이 시스템 은 Toast 를 표시 하고 숨 기기 위해 이 스 레 드 와 연 결 된 처리 프로그램 을 사용 합 니 다.우선,Toast 는 정확하게 표 시 됩 니 다.그러나 시스템 이 이 를 숨 기 려 고 할 때 onHandleIntent 방법 이 완료 되 었 습 니 다."sending message to a Handler on a dead thread"의 오 류 는 버 려 집 니 다.Toast 가 그 위 에 만 든 스 레 드 가 더 이상 유효 하지 않 고 Toast 가 사라 지지 않 기 때 문 입 니 다.
해결 방법:
// create a handler to post messages to the main thread
    Handler mHandler = new Handler(getMainLooper());
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
        }
    });

참조:http://www.xuebuyuan.com/36739.html

좋은 웹페이지 즐겨찾기