Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Thread and class behavior question
    text
    copied!<p>The project i am working on needs this type of behavior. The user will be presented with a UI that will give them the option to connect and disconnect to a server. I would also like this UI to show the status of the connection, 'connected or disconnected'. Whenever the user clicks connect, the application will start a thread that handles the connection to the server. The user will still be looking at the main UI. When they start that connection and while the connection remains, i would like the status of the connection to be 'connected'. If the connection is ever broken at any point i would like it to display disconnected. Listed below is what i have so far.</p> <p>My question is... Am i doing the threading right? So that the phone will not be crushed by the server connection while it is connected?</p> <p>Also, how do i get the main UI to reflect the connection status of the server and display when the connection is broken?</p> <p>Thanks in advance! If you have any further questions, please let me know.</p> <pre><code>The server connection thread. public class ConnectDevice implements Runnable { private boolean connected; private ObjectInputStream ois; public void run() { try { InetAddress host = InetAddress.getByName("192.168.234.1"); Socket socket = new Socket(host.getHostName(), 7777); connected = true; while (connected) { try { ois = new ObjectInputStream(socket.getInputStream()); String message = (String) ois.readObject(); System.out.println("Message: " + message); } catch (Exception e) { e.printStackTrace(); } } ois.close(); } catch (UnknownHostException e) { e.printStackTrace(); connected = false; } catch (IOException e) { e.printStackTrace(); connected = false; } /*catch (ClassNotFoundException e) { e.printStackTrace(); connected = false; }*/ } } The main UI and main class. public class SmartApp extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.intro); final Button firstTimeButton = (Button) findViewById(R.id.firstTimeButton); firstTimeButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent userCreationIntent = new Intent(v.getContext(), UserCreation.class); startActivityForResult(userCreationIntent, 0); } }); final Button connectDeviceButton = (Button) findViewById(R.id.connectDeviceButton); connectDeviceButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Intent connectDeviceIntent = new Intent(v.getContext(), ConnectDevice.class); //startActivityForResult(connectDeviceIntent, 0); Thread cThread = new Thread(new ConnectDevice()); cThread.start(); } }); } } </code></pre>
 

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