I know Bound Services Lives only until the Activity or any component needs it. We have to call the MyLocalBinder class from onBind function. Why is it so? Why can't we call it directly?
public class MyService extends Service {
private final IBinder myBinder = new MyLocalBinder();
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}
public String getCurrentTime(){
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss", Locale.UK);
return (df.format(new Date()));
}
public class MyLocalBinder extends Binder{
MyService getService(){
return MyService.this;
}
}
}
If you are using binder calls within the same application then it is a way of accessing some of your service class functionality from other components like activity.
If you are using binder api calls between applications this means your trying to use remote functionality from your application. Since two applications run in their own processes binder will acts like a pipe with security.
You can not call MyBinder class directly as you can not access service class members directly. Since bindservice provides a callback with binder object it is recommended to follow this approach.