title: Android Service killed by ANR
body: <p>I have a Service in my app (created by a thread), and inside I create another thread and that is a TCP server. 
  Short time after starting the thread (and fully working, receiving and handling the packages received) the service is <strong>killed</strong> by "timeout executing service" and ANR in my service.</p>
  
  <p>This is the code of my Service and the thread that is doing all the work:</p>
  
  <pre><code>public class Android_Wifi_Service extends Service {
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          super.onStartCommand(intent, flags, startId);
          handleStart();
          return START_STICKY;
      }
  
      private void handleStart() {
          Server server=new Server(this);
          new Thread(server).run();
      }
  
  
      @Override
      public void onDestroy(){
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
  }
  </code></pre>
  
  <p>And this is my thread:</p>
  
  <pre><code>public class Server implements Runnable {
      private boolean end=false;
      private Context context;
  
      public Server(Context _context){
          this.context=_context;
      }
  
      @Override
      public void run() {
          try {
              ServerSocket ss= new ServerSocket(5552);
  
              while(!end){
                  //wait for connection
                  System.out.println("Server Waiting connection");
                  Socket s=ss.accept();
                  SenderMessage msg=null;
                  // connected 
                  InputStream is = s.getInputStream();
                  ObjectInputStream obj=new ObjectInputStream(is);
                  try {
                      msg= ( SenderMessage ) obj.readObject();
                  } catch (Exception e) {
                      Log.e("Message Receive Error", e.toString());
                  }
                  obj.close();
                  is.close();
                  s.close();
                  if(msg!=null){
                      // do somethings
                  }
              }
  
          } catch (IOException e) {
              Log.e("Messages Server", e.toString());
          }
  
      }   
  }
  </code></pre>
  
  <p>And this is the logcat:</p>
  
  <pre><code>&gt; 05-26 13:48:39.019: W/ActivityManager(1364): Timeout executing
  &gt; service: ServiceRecord{407b1838
  &gt; wifi/.auxiliar.Android_Wifi_Service} 05-26
  &gt; 13:48:39.179: I/Process(1364): Sending signal. PID: 4129 SIG: 3 05-26
  &gt; 13:48:39.179: I/dalvikvm(4129): threadid=4: reacting to signal 3 05-26
  &gt; 13:48:39.219: I/dalvikvm(4129): Wrote stack traces to
  &gt; '/data/anr/traces.txt' 05-26 13:48:39.229: I/Process(1364): Sending
  &gt; signal. PID: 1364 SIG: 3 05-26 13:48:39.229: I/dalvikvm(1364):
  &gt; threadid=4: reacting to signal 3 05-26 13:48:39.329: I/dalvikvm(1364):
  &gt; Wrote stack traces to '/data/anr/traces.txt' 05-26 13:48:39.339:
  &gt; I/Process(1364): Sending signal. PID: 1452 SIG: 3 05-26 13:48:39.339:
  &gt; I/dalvikvm(1452): threadid=4: reacting to signal 3 05-26 13:48:39.379:
  &gt; I/dalvikvm(1452): Wrote stack traces to '/data/anr/traces.txt' 05-26
  &gt; 13:48:39.389: I/Process(1364): Sending signal. PID: 1433 SIG: 3 05-26
  &gt; 13:48:39.389: I/dalvikvm(1433): threadid=4: reacting to signal 3 05-26
  &gt; 13:48:39.419: I/dalvikvm(1433): Wrote stack traces to
  &gt; '/data/anr/traces.txt' 05-26 13:48:39.919: D/dalvikvm(1364):
  &gt; GC_CONCURRENT freed 1086K, 42% free 5485K/9351K, external 1223K/1735K,
  &gt; paused 7ms+12ms 05-26 13:48:40.089: D/dalvikvm(1364): GC_EXPLICIT
  &gt; freed 116K, 41% free 5529K/9351K, external 1223K/1735K, paused 108ms
  &gt; 05-26 13:48:40.829: E/ActivityManager(1364): ANR in
  &gt; wifi:Service 05-26 13:48:40.829:
  &gt; E/ActivityManager(1364): Reason: Executing service
  &gt; wifi/.auxiliar.Android_Wifi_Service
  </code></pre>
  
  <p>Can anyone help me?</p>