Android Tutorial: Part 3 Adding Client TCP/IP software to the Rapid Prototype GUI Project 5.2 1
Step 1: Copying the TCP/IP Client Source Code Quit Android Studio Copy the entire Android Studio project folder P5.1 to P5.2\v1 (create a new folder P5.2\v1). Copy the folders client, servermessagehandler, usercommandhandler, and userinterface from the Netbeans project folder P4.3\Client to the appropriate folder contained within the folder P5.2\v1. Be sure that source implements the changes discussed in class: protocol, interface, overloading, user command handler thread, setport, and setip. Start Andriod Studio and open the project P5.2/v1. Descend into each new java folder client, servermessagehandler, usercommandhandler, and userinterface and rename the packages, prepending com.ferens.ken (as appropriate) to every reference to the old package names. Note that changing the package names is the only change required to introduce the TCP/IP Client layer software to the Android GUI. 2
Step 2: Changes in the GUI Instantiate the TCP/IP Client Objects In the MainActivity.jav file, in method oncreate instantiate the classes Client and UserCommandHandler. Ensure that a myusercommandhandler reference variable is declared in the MainActivity class. This variable is needed by the GUI to send user commands to the handler. In the MainActivity.java file, specify that the MainActivity class implements com.ferens.ken.userinterface.userinterface (as appropriate). 3
Step 3.1: Changes in the GUI Implement the Update Method and Handler Create a concrete update() method. The body of this method should create a handler, as discussed in class, as follows: public void update(string thestring) { Message msg = new Message(); msg.obj = thestring; msg.settarget(myservermessagetextboxhandler); myservermessagetextboxhandler.sendmessage(msg); @SuppressLint("HandlerLeak") Handler myservermessagetextboxhandler=new Handler() { @Override public void handlemessage(message msg) { myservermessagetextbox.append(msg.obj +"\n"); ; 4
Step 3.2: Changes in the GUI Implement the Update Method and Handler Create a concrete update() method. The body of this method should create a handler, as discussed in class, as follows: public void update(string thestring) { ServerMessageTextBoxString = thestring; myservermessagetextboxhandler.sendemptymessage(7777); Handler myservermessagetextboxhandler=new Handler() { @Override public void handlemessage(message msg) { if(msg.what==7777) ; myservermessagetextbox.append(getservermessagetextboxstring()+"\n"); 5
Step 4: Changes in the GUI Server Messages Text BoX ID Declare a TextView instance variable myservermessagetextbox in the MainActivity class. In the oncreate method, get the ID of the server messages TextBox, as follows: myservermessagetextbox = (TextView)findViewById(R.id.ServerMessageTextBox); 6
Step 4: Changes in the GUI Server Messages Text BoX ID public class MainActivity extends AppCompatActivity implements com.ferens.ken.userinterface.userinterface { com.ferens.ken.usercommandhandler.usercommandhandler myusercommandhandler; TextView myservermessagetextbox; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // you may have other statements com.ferens.ken.client.client myclient = new com.ferens.ken.client.client ( (int)7777, (String)"130.179.231.109", this); myusercommandhandler = new com.ferens.ken.usercommandhandler. UserCommandHandler(this, myclient); myservermessagetextbox = (TextView)findViewById(R.id.ServerMessageTextBox); 7
Step 5: Changes in the GUI Permissions In the AndroidManifest.xml file, add permission (before the application xml tag) to use the Internet: //<uses-permission android:name="android.permission.access_wifi_state" /> //<uses-permission android:name="android.permission.change_wifi_state" /> //<uses-permission android:name="android.permission.change_network_state" /> <uses-permission android:name="android.permission.internet" /> //<uses-permission android:name="android.permission.access_network_state" /> 8
Step 6: Changes in the GUI User Command Handlers For each handler in your Client app, get the value of the widget and send the appropriate String message to the handleusercommand() method of the UserCommandHandler object. For example: public void portnumberbuttonhandler(view view) { EditText myporttextbox; String myport; myporttextbox = (EditText)findViewById( R.id.portNumberEditText); myport = myporttextbox.gettext().tostring(); myusercommandhandler.handleusercommand ("SetPortNumber "+myport); 9
Start the associated server, and take note of the IP address and the port number of your server. Find network IP address using ipconfig Or use 10.0.2.2 Run the app. You should have no errors and the emulator should be displaying your user interface. Enter the port number and IP Address of your server. You should get the server acknowledge message when you tap on the Connect button in your GUI client. 10