0

From the main activity of an android application, is it possible pass a Socket type parameter to a service upon starting the service?

user3705359
  • 49
  • 2
  • 5

1 Answers1

0

There are some ways to do that:

  1. Use global variable to store your socket instance, then in your service, just call it

    public class MyApplication extends Application {
       private static Socket socket;
       public Socket getSocket() {
        return socket;
       }
    
       public void setSomeVariable(Socket otherSocket) {
        this.socket = otherSocket;
       }
    }
    
  2. Use intent to pass and receive data field by field (or can use serializable or Parcelable). But this way will create other instance of socket

  3. Use event bus to send and receive from activity to service

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165