Domanda

I have a Service and when there is a screen rotation, It is bound again but onServiceConnected on ServiceConnection() is not called so my deviceController is null.

Is there any way to get the bound service ?

Here the code :

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
}
È stato utile?

Soluzione

It could be that the unBindService is not called when orientation change.

It would be good to bind service in onStart() and unbind in onStop(). So that when the Activity is not visible , You don't really requited the service connection.

This will make sure the service is unbind when not required.

Check this http://developer.android.com/guide/components/bound-services.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top