2014-04-30 3 views
2

Service이 있고 화면 회전이있을 때 다시 바운드됩니다. onServiceConnected on ServiceConnection()이 호출되지 않아 내 deviceController가 null입니다.화면 회전 후 서비스 받기

바운드 서비스를받을 방법이 있습니까? 여기에 코드

:

private MyService myService; 
private ServiceConnection myServiceConnection = new ServiceConnection() 
{ 
    public void onServiceConnected(ComponentName name, IBinder service) 
    { 
     MyActivity.this.myService = ((MyService.LocalBinder) service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName name) 
    { 
     MyActivity.this.myService = null; 
    } 
}; 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{ 
    // code to bind service 
} 

답변

1

그것은 unBindService이 때 방향 변경 호출되지 수 있습니다.

onStart()에 서비스를 바인딩하고 onStop()에 바인딩 해제하는 것이 좋습니다. 따라서 Activity이 보이지 않으면 실제로 서비스 연결을 요청하지 않은 것입니다.

이렇게하면 서비스가 필요하지 않을 때 바인딩 해제됩니다.

확인이 http://developer.android.com/guide/components/bound-services.html

+0

호 '사용자 활동이 활동과 상호 작용할 수있는'onResume'이어서 사용자에게 표시 될 때 onStart' 호출된다. – Libin