title: Android cannot connect socket from background service
body: <p>I have a broadcaster that receives boot complete broadcast and start a service that connects to a server socket. If I start the service from the main activity the socket can connect, but when the service is started from the broadcast receiver, it cannot connect, simply wait and then timed out exception occurs.. </p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
          if (intent.getAction().compareTo(Intent.ACTION_BOOT_COMPLETED) == 0){
              Log.d(TAG, "MyReciever.onReceive(ACTION_BOOT_COMPLETED)");
              context.startService(new Intent(context, MyService.class));
          }else if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
              Log.d(TAG, "MyReciever.onReceive(ACTION_TIME_TICK)");
          }else{
              Log.d(TAG, "MyReciever.onReceive(" + intent.getAction() + ")");
          }
      }
  </code></pre>
  
  <p>This is my broadcasreciever. I can see in logcat the service is started, but the socket connection fails.. If I do this in the main activity..</p>
  
  <pre><code>startService(new Intent(this, MyService.class));
  </code></pre>
  
  <p>Here is the simple socket code that resides in MyService within a Handler and invoked using that handler.</p>
  
  <pre><code>private boolean ConnectSocket() {
              try {
                  Log.d(TAG, "Connecting to server ...");
                  socket = new Socket();
                  SocketAddress socketAddress=new InetSocketAddress("192.168.1.6", 5656);
                  socket.connect(socketAddress, 2000);
                  socketWriter = new PrintWriter(socket.getOutputStream(), true);
                  Log.d(TAG,"Connected to server!");
                  return true;
              } catch (Exception ex) {
                  Log.e(TAG, "Error in connecting socket");
                  Log.e(TAG, ex.getStackTrace().toString());
                  return false;
              }
   }
  
  private final class ServiceHandler extends Handler {
          public ServiceHandler(Looper looper) {
              super(looper);
          }
          @Override
          public void handleMessage(Message msg) {
              this.ConnectSocket();
          }
  }
  </code></pre>
  
  <p>Here is the service</p>
  
  <pre><code>@Override
      public void onCreate() {
          super.onCreate();
          Log.d(TAG, "MySerivce.onCreate()");
          registerReceiver(new MyReceiver(), new IntentFilter(Intent.ACTION_TIME_TICK));
          HandlerThread thread = new HandlerThread("ServiceStartArguments",
                  android.os.Process.THREAD_PRIORITY_BACKGROUND);
          thread.start();
          serviceLooper = thread.getLooper();
          serviceHandler = new ServiceHandler(serviceLooper);
      }
  
  public int onStartCommand(Intent intent, int flags, int startId) {
              if(!this._started) {
                  this._started = true;
                  Log.d(TAG, "MySerivce.onStartCommand()");
                  Message msg = serviceHandler.obtainMessage();
                  msg.arg1 = startId;
                  serviceHandler.sendMessage(msg);
              }
              return START_STICKY;
          }
  </code></pre>
  
  <p>Here is the stack strace</p>
  
  <pre><code>2020-04-21 13:28:05.987 8275-8315/x.y.z E/TechCapture: Error in connecting socket
      java.net.SocketTimeoutException: failed to connect to /192.168.1.6 (port 5656) from /192.168.1.3 (port 37956) after 2000ms
          at libcore.io.IoBridge.connectErrno(IoBridge.java:185)
          at libcore.io.IoBridge.connect(IoBridge.java:129)
          at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
          at java.net.Socket.connect(Socket.java:621)
          at x.y.z.MyService$ServiceHandler.ConnectSocket(MyService.java:80)
          at x.y.z.MyService$ServiceHandler.handleMessage(MyService.java:45)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:201)
          at android.os.HandlerThread.run(HandlerThread.java:65)
  </code></pre>
  
  <p>Then the socket can connect to the server as usual. Is there any android restriction in place?</p>