I've got a service meant to sometimes run in the background - started with startService(). What is the advantage of binding to this service so as to get/set its variables, instead of - controversial, I know, but still - just accessing its public variables directly (e.g. myVar = mainService.itsVar), or using SharedPrefs to set and get the values? Especially, what is the fastest, in terms of performance, based on the fact that the get interval would be roughly 3 seconds?
2 Answers
Advantage - You can access variables and run methods directly. Disadvantage - allot of cross thread headache, not to mention unresolved crashes on google developer console.
How to do it correctly, I will give example of a foreground service, if it can be background then change only how to start it:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mContext.startForegroundService(startIntent)
} else {
mContext.startService(startIntent);
}
In the service you need to implement all binding methods:
private IBinder mBinder = new MyBinder();
public MainService() {
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
public class MyBinder extends Binder {
public MainService getService() {
return com.xxx.xxx.MainService.this;
}
}
In the activity you need to listen to connection:
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBoundService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "connecting");
MainService.MyBinder myBinder = (MainService.MyBinder) service;
mBoundService = myBinder.getService();
mShouldUnbind.set(true);
}
};
If other activity or alarm can start your service you will need to check every second or so if the service was started by other and bind to it. Important - service run on the same thread like the main/GUI thread - so if you have time consumption actions like camera or uploading - you will need to start a background thread. back communication can be made by calling the GUI/main thread. on foreground service like mine - you need also to manage notifications otherwise the o.s. will kill the service after 5 seconds.
Code example:
if (mShouldUnbind.get() && mBoundService != null)
val = mBoundService.getTimestamp();
Edit I: advantage: I have a camera foreground service - I can set zoom values directly from the GUI thread since I have a handle to the service. the camera is running on the background thread in the service, but, since the preview is being drawn 30 times pro seconds - the zoom is set without any lag. you will never manage to do it in any other way.

- 635
- 8
- 27
-
thanks Kfir, and hello again! +1 but I am letting the question unanswered as I am after a deeper explanation of advantages vs. drawbacks from a variables perspective between the 3 options (e.g. what's the point of accessing the variables "directly"?). – Razvan_TK9692 Sep 13 '21 at 21:11
-
1hello again, since you helped me so much - wanted to give share back some knowledge. and I saw you have this old question waiting for answers. no stress about approving the answer. just wanted to share back from my experience. I will anyhow add some more info to the answer – kfir Sep 14 '21 at 07:08
-
I find it's a great example of the relevance of binding to a service to SET its variables; more generally, I realise it has to do with threading optimisation, by the looks of it. I was originally interested in the differences involved for the purpose of GETTING variables - I'll look into it a bit more and see whether threading optimisation done in this way could apply to my own use cases. At the time I did bind to the service, and got the variables the right way, but always felt it was a bit over the top. apparently not. thanks – Razvan_TK9692 Sep 16 '21 at 19:09
there is a difference between bounded service and Unbound service, bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed, see the below link
Can anybody explain what is difference between unbound and bound service in android

- 416
- 3
- 9
-
thanks Fahad. I have edited my question, to mention that I have an unbound service to begin with (started with startService()). I would like to know the fastest way to get the variables of that service, from an activity: binding to it, or via SharedPrefs, or accessing public variables directly. Basically, what is the use of binding to an unbound service? – Razvan_TK9692 Mar 18 '20 at 15:25