Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent the application timeout?
    text
    copied!<p>I have created a multithreaded application but it still hangs if the server is unavailable.</p> <p>In the main Activity I've created the following methods:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //bind GUI elements ... //start TCPConnection Service in a new thread establishTCPConnection(); ... } private void establishTCPConnection(){ TCPClientServiceThread = new Thread(null, backgroundConnection, "connection"); TCPClientServiceThread.start(); } .. private Runnable backgroundConnection = new Runnable(){ public void run(){ //establish TCP connection doEstablishTCPConnection(); } }; private void doEstablishTCPConnection() { //start TCPConnection service startService(new Intent(this, TCPClientService.class)); } </code></pre> <p>And this is the TCPClientService Class:</p> <pre><code>public class TCPClientService extends Service{ ... private String serverAddress = "192.168.1.5"; private int portNumber = 1000; @Override public void onCreate() { //TODO: Actions to perform when service is created connectionAvailable = false; IntentFilter dataReceiverFilter; dataReceiverFilter = new IntentFilter(MotranetClient.MOTION_DATA_UPDATED); dataReceiver = new DataReceiver(); registerReceiver(dataReceiver, dataReceiverFilter); EstablishConnection(); } @Override public IBinder onBind(Intent intent) { //TODO: Replace with service binding implementation return null; } private void EstablishConnection() { try { InetAddress serverAddr = InetAddress.getByName(serverAddress); Log.d("TCP", "C: Connecting..."); Socket socket = new Socket(serverAddr, portNumber); String message = "testing connection"; try { Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); out.println(message); Log.d("TCP", "C: Sent."); Log.d("TCP", "C: Done."); connectionAvailable = true; } catch(Exception e) { Log.e("TCP", "S: Error", e); connectionAvailable = false; } finally { socket.close(); announceNetworkAvailability(connectionAvailable); } } catch (Exception e) { Log.e("TCP", "C: Error", e); announceNetworkAvailability(connectionAvailable); } } } </code></pre> <p>When the server is unavailable the line </p> <pre><code>Socket socket = new Socket(serverAddr, portNumber); </code></pre> <p>causes some delay and I believe this is the cause for hanging. But if the TCPClientService Service is executing in its own thread I don't know why this affects the timeout of the main activity.</p> <p>I would be very thankful if anyone could show how to prevent the application from hanging when the server is unavailable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload