요즘 TabActivity를 기본으로해서 개발을 하다보니 의외로 관련 자료나 내용이 적고
도움이 되는 소스를 요청하는 분은 많은데 실제 자료가 거의 없는 상황이라 앞으로 이 부분에
관한 정보를 좀 올릴려고 합니다. 많이 지켜봐 주세요..
오늘은 1탄으로 TabActivity 내에서 ActivityGroup을 사용하여 하위 Activity를 관리할 때
Service를 Binding 하는 방법에 대한 내용입니다.
일반적인 Service 구동 방법이나 Binding 방법은 많은 분이 자료를 올려 두셨으므로 여기서는
핵심 내용만 쓰겠습니다.
(일반적인 오류)
Intent intent = new Intent(this,TwitterService.class); boolean result = bindService(intent, twitterConnection, Context.BIND_AUTO_CREATE ); if(result)Log.d("MK","Binding Twitter Service Success......"); else Log.d("MK","Binding Twitter Service Failed..."); |
(올바른 사용)
Intent intent = new Intent(getApplicationContext(),TwitterService.class); boolean result = getApplicationContext().bindService(intent, twitterConnection, Context.BIND_AUTO_CREATE ); if(result)Log.d("MK","Binding Twitter Service Success......"); else Log.d("MK","Binding Twitter Service Failed..."); |
위의 “일반적인 오류”의 경우는 일반적인 Activity에서는 Binder를 통해 Service 객체를 Binding 할 수 있으므로 오류가 없는 것으로 보일 수 있다.
하지만 TabActivity를 활용한 GroupActivity를 사용하여 관리되는 Child Activity의 경우에는
Binding with unknown activity: android.os.BinderProxy@43535e18와 같은 오류를 만나게 된다.
이는 Service에 대해서 Binding을 요청하는 Client Activity가 GroupActivityManager에 의해서 관리 되므로 Binder가 해당 Client Activity의 Context에 직접 접근을 하지 못하므로 Binding된 Service를 넘겨줄 수 없는
이유에서 비롯된다.
따라서 Context를 Child Activity 대신 ApplicationContext로 Binding 된 Service 객체를 직접 전달함으로 그 하위에 관리되는 Child Activity 에 연결을 위임해야 정상적으로 Client Activity와 Service가 Binding 될 수 있다.
작은 도움이라도 되었으면 좋겠구요
2탄은 TabActivity를 사용하여 다양한 Activity를 연동할 경우 Navigation 처리(다음처리/이전처리) 등을 가지고
예제와 설명을 올리겠습니다.
많은 기대 부탁 드립니다..^^