Reputation: 1089
I have a service that is used by the main activity to do some background job and it's started using binding and unbinding.
In this way, when the activity start the service start and stop when the activity call unbind, this work well.
Now I need to have a CheckBoxpreference that once true will start the service and it must run in background despite the main activity will be close.
What is the best way to get this result? do I have to work on startservice/stopservice from preferences or I have to don't unbind from main activity when the preference is true?
if I start the service from the preference when already started from the binding of main activity, this will be multiple instance of same service ?
Thank you
Upvotes: 2
Views: 936
Reputation: 15619
Use a OnSharedPreferenceChangeListener
so you'll get updates when the checkbox changes from selected to unselected (and vice versa).
You can use both the start/stopService mechanism and the bind/unbind mechanism at the same time. The service will start if either startService is called or if the service is bound, it will stop when stopService is called AND no activities are bound anymore.
I think this post will answer your question regarding services that have already been started.
Upvotes: 2