Search code examples
androidandroid-serviceandroid-service-binding

Android Service and its engine


I have one doubt about using services. I have a service that initializes an object, is it a bad practice to pass an instance of the service to the object so it can be used for that object? A simplified object would be:

public class MyService extends Service {
    MyObject myObject = new MyObject(this);


    ...
}

public MyObject {
    private MyService myService;

    public MyObject(MyService myService) {
        this.myService = myService;
    }

    ...
    private void exampleMethod() {
        myService.method();
    }
}

What do you think? Is it a bad practice? How could I solve that issue without passing the service's instance? The fact is that I want to split the code in two classes because the features are different, the websocket is connected from the service class, but the methods to parse/send events through the websocket are in the second class. I want to do this way in order to avoid having one class with 2000 lines of code, and by splitting the code by features. The service handles the websocket connection, while the other class handles the other features. As everything is asynchronous, the second class needs an instance of the service class. For instance: if an error is received and parsed (on the second class), this second class must call the service class to update its status and do a reconnection.

EDIT: I'm thinking about implementing the following solution:

public class MyService extends Service {
    MyObject myObject = new MyObject() {
          protected void onSuccess() {
              ...
          }
    };


    ...
}

public abstract class MyObject {


    public MyObject() {
    }

    protected abstract void onSuccess();

    ...
    private void exampleMethod() {
        ...
        onSuccess()
    }
}

The more I think about it, the better solution I think it is. What do you think?

Thank you very much in advance!


Solution

  • This makes no sense at all. I suggest you to use a interface if you need to pass a callback to a dao (the websocket controller). The thing is that you should use your service to implement your websocket controller. Please add the websocket code, so we can suggest more changes.

    EDIT:

      public interface onGetData {
    
        public void onSuccess(Object response) // here you pass the obj type you need in your service
    
        public void onError(Object error) // same, but if things fail
    
      }
    
    
    
     public class MyService extends Service implements onGetData  {
    
        @Override
        public void OnCreate() {
           MyObject myObject = new MyObject(this);
        }
    
        @Override
        public void onSuccess(Object response) {
        }
    
        @Override
        public void onError(Object error) {
        }
    
    
     }
    
    
    public MyObject {
      private OnGetData onGetData ;
    
      public MyObject(OnGetData onGetData) {
        this.onGetData = onGetData;
      }
    
      private void onRequestSuccess(Object response) {
        onGetData.onSuccess(response)
      }
    
      private void onRequestError(Object error) {
        onGetData.onError(error)
      }
    }