I\'m trying to work out if bound service is appropriate for doing background work in my app. The requirements are that various application components can make web requests throu
Can you not just select configurations that you would like to handle with the configChanges attribute in your manifest and do the orientation changes in the UI manually? In this case you only need to bind to the service in onCreate
and then unBind in onDestroy
.
or maybe try something like this ( I have not done proper error checking):
class MyServiceConnection implements ServiceConnection,Parcelable { public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public MyServiceConnection createFromParcel(Parcel in) { return new MyServiceConnection(in); } public MyServiceConnection[] newArray(int size) { return new MyServiceConnection[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { } } MyServiceConnection myServiceConnection; boolean configChange = false; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { myServiceConnection = savedInstanceState.getParcelable("serviceConnection"); } else { myServiceConnection = new MyServiceConnection(); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (myServiceConnection != null) { outState.putParcelable("serviceConnection",myServiceConnection); configChange = true; } } @Override protected void onDestroy() { super.onDestroy(); if (!configChange && myServiceConnection != null){ unbindService(myServiceConnection); } } }