Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android thread in service issue

Tags:

android

I am having a remote service running in which i am spawning a new thread to keep polling for incoming messages via socket and then at certain intervals i want to stop the socket and restart later...stopping is working fine (stopUARTConnection()) but re starting(startUARTConnection) is not causing the while loop again.. Can someone tell me whats wrong ?

public class BackgroundService extends Service{

ServerSocket server = null;
Socket socketClient = null;
InputStream is = null;
BufferedReader br = null;
char[] buff = null;
boolean threadRunning = true;

private static final String TAG = BackgroundService.class.getSimpleName();
private Thread socketThread = null;
int temp = 0;

@Override
public IBinder onBind(Intent intent) {
    if (BackgroundService.class.getName().equals(intent.getAction())) {
        Log.d(TAG, "Bound by intent " + intent);
        return apiEndpoint;
    } else {
        return null;
    }
}

private final Object latestIncomingMessage = new Object();
private List<MessageCollectorListener> listeners = new ArrayList<MessageCollectorListener>();
private Message message = new Message(" ");

private MessageCollectorApi.Stub apiEndpoint = new MessageCollectorApi.Stub() {
    @Override
    public void removeListener(MessageCollectorListener listener) throws RemoteException {
        synchronized (listener) {
            listeners.remove(listener);
        }
    }

    @Override
    public Message getValue() throws RemoteException {
        synchronized (latestIncomingMessage) {
            return message;
        }
    }

    @Override
    public void addListener(MessageCollectorListener listener) throws RemoteException {
        synchronized (listener) {
            listeners.add(listener);
        }
    }

    @Override
    public void startBroadCastConn() throws RemoteException {
        try {
            startUARTConnection();
        } catch (IOException e) {
            Log.d("B Service", "Stop UART CONNECTION");
        }

    }

    @Override
    public void stopBroadCastConn() throws RemoteException {
        try {
            stopUARTConnection();
        } catch (IOException e) {
            Log.d("B Service", "Stop UART CONNECTION");
        }           
    }
};

public boolean createSocket(int port) {
    try{
        server = new ServerSocket(port);
    } catch (IOException e) {
        return false;
    }
    return true;
}

public boolean listenSocket(){
    try{
        socketClient = server.accept();
    } catch (IOException e) {
        return false;
    }
    return true;
}

@Override
public void onCreate() {
    super.onCreate();
    socketThread = new Thread(){
        @Override
        public void run() {
            while(threadRunning){
                boolean socketCreated = createSocket(8080);
                boolean socketListen = listenSocket();
                if(socketListen == true && socketCreated == true){
                    //To add code for processing data..
                    threadRunning = true;
                }
                try {
                    recycle();
                } catch (IOException e) {
                    //e.printStackTrace();
                }
            }
        }   
    };
    socketThread.start();
}
public void stopUARTConnection() throws IOException{
    recycle();
    threadRunning = false;
}

public void startUARTConnection() throws IOException{
    recycle();
    threadRunning = true;
}

private void recycle() throws IOException{
    if(socketClient != null){
        socketClient.close();
        socketClient = null;
    }   
    if(is != null)
        is.close();
    if(server != null){
        server.close();
        server = null;
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    try {
        recycle();
    } catch (IOException e) {
        Log.d("B Service", "Failed to recycle all values");
    }
    socketThread = null;
}

}

like image 455
Arun Abraham Avatar asked Apr 21 '26 17:04

Arun Abraham


1 Answers

You are creating the Thread only once. The loop is thus run only once until it is instructed to exit.

public void startUARTConnection() throws IOException{
    recycle();
    socketThread = new SocketThread();
    socketThread.start();
}

public void stopUARTConnection() throws IOException{
    recycle();
    socketThread = null;
}

private class SocketThread extends Thread {
    @Override
    public void run() {
        // Loop until socketThread is assigned something else 
        // (a new Thread instance or simply null)
        while (socketThread==this){
            boolean socketCreated = createSocket(8080);
            boolean socketListen = listenSocket();
            if(socketListen == true && socketCreated == true){
                // To add code for processing data..
            }
            try {
                recycle();
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
    }   
};
like image 182
Vincent Mimoun-Prat Avatar answered Apr 24 '26 06:04

Vincent Mimoun-Prat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!