开发者

Android Thread and class behavior question

The project i am working on needs this type of behavior. The user will be presented with a UI that will give th开发者_开发问答em 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.

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?

Also, how do i get the main UI to reflect the connection status of the server and display when the connection is broken?

Thanks in advance! If you have any further questions, please let me know.

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


Android has a UI thread, which is the only thread that is allowed to update UI elements.

You need to have a background thread doing the work, and posting back to the UI thread when its done. AsyncTask is an Android class designed to do just that.

Once your worker thread ends its work, and will update the UI element taken by findViewById, it will automatically change on the screen without you having to do anything else.


Check out the AsyncTask, it's tailor made for this sort of thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜