I have a such Service:
public MyService extends Service {
// ...
IBinder binder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
// ...
}
In Activity I receive Binder thereby get Service instance, and after that I have access to all its methods. I want to know, is it safe to do like that? Or I should interact with Service only through Binder interface? Thank you!
The Binder is what gets returned and you simply cast to the Service class you know it to be. The way that you’re doing it IS only using the Binder…
And the way you’ve done it is typically how it’s done. It’s the “local service” pattern taken directly from the “official” sample found here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html Other ways of calling methods on your Service class are quite hacky (believe me, I’ve tried them before).
Example: