1

I am working on a Chat messenger.I have used socket.io-client library.I have created a service that will instantiate Socket class.

SocketService.java

    public class SocketService extends Service {

    public static Socket mSocket;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            String data = intent.getStringExtra("Socket");
            Log.e("intent data", data);
            Log.e("Service", "Called");
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Service", "created");
        try {
            mSocket = IO.socket(Constants.CHAT_SERVER_URL);
            Log.e("ValueSocket", String.valueOf(mSocket));

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

I am starting the service in SplashActivity.java.I have to pass Socket instance i.e. mSocket to Activity SplashActivity.java .How can i achieve this in android?Please help .

Deepak Rattan
  • 1,279
  • 7
  • 21
  • 47
  • Bind your service from Activity. This link might help you. http://stackoverflow.com/questions/23282367/best-practice-for-pass-info-from-service-to-activity-or-fragment – Febi M Felix Mar 10 '16 at 09:24
  • yep, return custom `Binder` from `onBind` method so that the client activity could call `bindService` – pskink Mar 10 '16 at 10:31

0 Answers0