In this blog, we going to discuss the Bound services in android.
Android services are the components that run in the background and perform any particular operation. Services run on the main thread until or unless we define the particular thread for the execution of the operation that we want to perform with the services.
Services are basically used to execute the log running task so that the task can be executed in the background while the same user is working or interacting with another part of the application.
The task running on the services must be a non-blocking task that can not harm the main performance of the application.
Storing data into the database and downloading files from the is the basic example of android services.
Bound service is the basically the implementation of the service class. It allow the other application to bind with the services and interact with it.
Bound service work as client server mode. It allows the components to bind for the services.
components can sent request and receive response which will perform interprocess communication.
Implementation
Create your bound service class like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class MyBoundService extends Service { public MyBoundService() { } private final IBinder serviceBinder = new MyBinder(); @Override public IBinder onBind(Intent intent) { return serviceBinder; } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public void onRebind(Intent intent) { super.onRebind(intent); } @Override public void onDestroy() { super.onDestroy(); } public String returnData(){ return "data"; } public class MyBoundServiceBinder extends Binder { public MyBoundService getService() { return MyBoundService.this; } } } |
We have extended the Service class and implementing the service class methods.
Use the service for your component like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
MyBoundService myBoundService; boolean isBoundService = false; @Override protected void onStart() { super.onStart(); Intent intent = new Intent(this , MyBoundService.class); startService(intent); bindService(intent , boundServiceConnection,BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); if(isBoundService){ unbindService(boundServiceConnection); isBoundService = false; } } private ServiceConnection boundServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { MyBoundService.MyBoundServiceBinder binderBridge = (MyBoundService.MyBoundServiceBinder) service ; myBoundService = binderBridge.getService(); isBoundService = true; } @Override public void onServiceDisconnected(ComponentName name) { isBoundService = false; myBoundService = null; } }; |
Implementation
Conclusion:
In this blog, we have learned about the implementation of the Bound services in android.
For more information regarding the Bound services in android follow the link
Thanks for reading this blog. You can also check other blogs from here.