Eduardo Soares
Eduardo Soares

Reputation: 13

Android Service killed by ANR

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 killed by "timeout executing service" and ANR in my service.

This is the code of my Service and the thread that is doing all the work:

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;
    }

}

And this is my thread:

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());
        }

    }   
}

And this is the logcat:

> 05-26 13:48:39.019: W/ActivityManager(1364): Timeout executing
> service: ServiceRecord{407b1838
> wifi/.auxiliar.Android_Wifi_Service} 05-26
> 13:48:39.179: I/Process(1364): Sending signal. PID: 4129 SIG: 3 05-26
> 13:48:39.179: I/dalvikvm(4129): threadid=4: reacting to signal 3 05-26
> 13:48:39.219: I/dalvikvm(4129): Wrote stack traces to
> '/data/anr/traces.txt' 05-26 13:48:39.229: I/Process(1364): Sending
> signal. PID: 1364 SIG: 3 05-26 13:48:39.229: I/dalvikvm(1364):
> threadid=4: reacting to signal 3 05-26 13:48:39.329: I/dalvikvm(1364):
> Wrote stack traces to '/data/anr/traces.txt' 05-26 13:48:39.339:
> I/Process(1364): Sending signal. PID: 1452 SIG: 3 05-26 13:48:39.339:
> I/dalvikvm(1452): threadid=4: reacting to signal 3 05-26 13:48:39.379:
> I/dalvikvm(1452): Wrote stack traces to '/data/anr/traces.txt' 05-26
> 13:48:39.389: I/Process(1364): Sending signal. PID: 1433 SIG: 3 05-26
> 13:48:39.389: I/dalvikvm(1433): threadid=4: reacting to signal 3 05-26
> 13:48:39.419: I/dalvikvm(1433): Wrote stack traces to
> '/data/anr/traces.txt' 05-26 13:48:39.919: D/dalvikvm(1364):
> GC_CONCURRENT freed 1086K, 42% free 5485K/9351K, external 1223K/1735K,
> paused 7ms+12ms 05-26 13:48:40.089: D/dalvikvm(1364): GC_EXPLICIT
> freed 116K, 41% free 5529K/9351K, external 1223K/1735K, paused 108ms
> 05-26 13:48:40.829: E/ActivityManager(1364): ANR in
> wifi:Service 05-26 13:48:40.829:
> E/ActivityManager(1364): Reason: Executing service
> wifi/.auxiliar.Android_Wifi_Service

Can anyone help me?

Upvotes: 1

Views: 8539

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007349

You do not start a Thread by calling run(). You start a Thread by calling start(). Right now, you are simply calling your own Thread's run() method on the main application thread.

Upvotes: 8

Related Questions