Declare and O implement a Handler


final TextView statuslabel = (TextView) findViewByld(

h = new Handler() { @Override public void handleMessage(Message msg) { Q Declare and switch (msg.what) { | implement a Handler case 0 :

Log.d("CH13","data [" + (String) msg.obj + "]"); statuslabel.setText((String) msg.obj); break;


Button test = (Button) findViewByld (R. id. testit); © Implement test. setOnClickListener (new Button. OnClickListener () { <1—1 click listener public void onClick(View v) { try {

Requester r = new Requester () ; <—© Create a Requester instance r.start(); } catch (Exception e) {

Log.d("CH13 exception caught : ", e.getMessage ())

This application is all about detecting the selection of a button © and initiating an action based on that click. The action is the creation of an instance of the Requester class ©, which we discuss in the next section. We handle the response from the socket server with the assistance of a Handler B. The Handler has a single role, which is to update the UI with textual data stored in the obj member of a Message object.

While the UI of this application is very simple, the more interesting side of this Activity is the interaction with the Daytime Server, which takes place in the Requester class, discussed in the next section.

0 0

Post a comment