hola amigos’, how are you guys doing? im fine thank you!. Status Update: About last week, I have covered some ground interms of development in old passion projects, the start of the week was great, but at the end of the week it got worse, nevertheless im happy that i survived this week >_<. Saw avengers end game the other day which was cool, during part-I I was studying, but for part-II -an adult going to office (feels wierd even to think, So much change in just one year), Personally nothing great happened, because of the mind that i have, I had the same bs.
> I’m gona explain about how to communicate with android services.
why? backstory..
B’coz when I was at the begining stages of my android life, I had some use case that i had to interact with service, I know java somebit but I was no java expert (haven’t done any big projects in java YET >.> (you wouldn’t know)). So without any research I went staright in, I thought i could do with interface with my own implementation that came to an dead end with problems. Later i found out binders.. stupid me.
How?
Well let me show you. First of all you must know about IBinder interface (available since android’s starting stages), then you should know about Binder class which implements the IBinder interface, keep in mind there are a lot of other ways around (as i have already mentioned in the previous posts).
It will be better if i explain this in code, as it will be much easier to understand.
create a class extending the class binder.
public class CustomBinderName extends Binder {
private sericeName serviceNameInterface;
/**
* gets Serivce context for each service,
*/
public CustomBinderName(Context serviceContext){
serviceNameInterface = (serviceName) serviceContext;
}
/**
* getService,
* return serviceNameInterface(this instance), which is what we need.
* calling contract - which we use in Activity.
*/
public serviceName getService(){
return serviceNameInterface;
}
}
then implementing serviceConnection.
The android os calls the contract methods inside this class (interface after implementation), these methods are called from the main thread of the application, like many other callbacks from the system.
/**
* ServiceConnection interface,
* calling bindService - contract - our custom implementation,
* calls onserviceConnection with componenetName and binder,
*
* we bind it with customClass implementation of customBinderNameBinder
* through the link, we get and save service on local serviceBoundLink.
*
*/
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
CustomBinderName customBinderName = (CustomBinderName) iBinder;
serviceBoundLink = customBinderName.getService();
isServiceBound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
isServiceBound = false;
}
};
}
Implementing necessary classes on the service.
/**
* needs to return Interface Ibinder,
* onBind initiated on bindService (creation of service),
*/
@Override
public IBinder onBind(Intent intent) {
return customBinderName;
}
/**
* Need to be of type IBinder.
* CustomBinderName is constructed such a was,
* instance of customBinderName has to be passed in
* via constructor, since this is how we implemented it.
*/
private final IBinder customBinderName = new CustomBinderName(this);
Binding the service, with some activity.
During service creation call bindService, passing in our implementation of serviceConnection, then the global BIND_AUTO_CREATE (usually bind_auto_create, since most of the time, service is not created before at this point), the system will take care of the rest.
/*
* intent for the service, with class,
* After start, bind the service(link) with local intance so its
* (service) methods can be called.
*/
bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);
Be sure to remove unbind the service, connection.. else you’ll end up with memory leaking.
bye see ya next time, by the time im making this post public, eveything changed to the polar opposite.