7.6 Writing the Code for the Android Client

You might be wondering why you should go through the trouble of building a native Android client when the web application we wrote can be accessed by the Android mobile web browser. Well, if all you wanted to do was toggle light switches on and off, then I would say you don’t need a native client. The web interface works just fine and can be further enhanced using AJAX and slick HTML5/CSS3 user interface effects. But if you want to give a little more intelligence to the app, such as activating power switches based on your proximity to them or running an Android service that monitors inbound X10 events like motion detection and then sounds an alert on your phone to bring such events to your attention, a dynamic web page just won’t do.

If you haven’t already done so, download, install, and configure the Eclipse IDE, the latest Android SDK, and the ADK plug-in for Eclipse. Visit the Android SDK website for details on how to do so.[73]

You will also need to create an Android Virtual Device (AVD) so that you can use it to test the client application in an Android emulator before sending the program to your Android device.[74] I suggest creating an AVD that targets Android 1.5 (API Level 3) so that it emulates the largest number of Android phones available.

Launch the Eclipse environment and select FileNewAndroid Project. Depending on the version of Eclipse you are running, this option might also be found on the File menu via New->Other->Android->Android Project. Call the project LightSwitch and select Build Target as Android 1.5. You can choose a higher Android version depending on what level of Android device you want to deploy the application to, but since the LightSwitch program will be sweet and simple, Android 1.5 should be adequate for this sample application.

In the Properties area, fill in the Application name as Light Switch and the Package name as com.mysampleapp.lightswitch, and check the Create Activity checkbox and enter LightSwitch. You can specify the Min SDK Version if you wish, but since we’re developing for one of the more popular lowest-common-denominator versions of Android, we’ll leave it blank for now. Before you continue, check to see if your New Android Project dialog box looks like the one shown in Figure 26, Creating a new Android Project dialog box with completed parameters.

/epubstore/R/M-Riley/Programming-your-home//images/newandroidx10project.png

Figure 26. Creating a new Android Project dialog box with completed parameters

Android developers with good testing practices would then click the Next button in the New Android Project dialog box to set up a Test Project resource. However, in the interest of space and time, we’ll go ahead and click the Finish button.

Once the Android Development Tools Eclipse plug-in generates the skeleton Light Switch application code, double-click the main.xml in the res/layout folder to open it into Android’s simple form editor. Drag a ToggleButton control from the Form Widgets palette onto the main.xml graphical layout. Don’t worry about perfectly aligning the control in the right spot for now. For this exercise, we’re more interested in function over form.

/epubstore/R/M-Riley/Programming-your-home//images/x10formlayout.png

Figure 27. The graphical form layout of the Light Switch application

Because this application won’t require anything beyond the basic features found in the earlier Android operating system releases, change the Android version in the upper right corner drop-down box of the form editor to Android 1.5. Also, feel free to delete the default Hello world TextView element from the layout. When done, the layout should look similar to the screen shown in Figure 27, The graphical form layout of the Light Switch application. Save the main.xml file.

Expand the srccom.mysampleapp.lightswitch tree and double-click the LightSwitch.java file. Because we will be using the ToggleSwitch widget, the first thing we need to import is the android.widget.ToggleButton class.

Next, add the java.net.URL and java.io.InputStream libraries, since we’ll be creating URL objects to pass to Java InputStream object. The import statement section of the LightSwitch.java file should now look like this:

  package com.mysampleapp.lightswitch;​
  import android.app.Activity;​
  import android.os.Bundle;​
  import android.widget.ToggleButton;​
  import android.view.View;​
  import java.net.URL;​
  import java.io.InputStream;​

Now we have to make the LightSwitch aware of the ToggleSwitch by finding it by ID in the LightSwitch class’s OnCreate event and adding an event listener to monitor when the switch is toggled on and off:

  public class LightSwitch extends Activity {​
  /** Called when the activity is first created. */
  ​ @Override​
  public void onCreate(Bundle savedInstanceState) {​
  ​ super.onCreate(savedInstanceState);​
  ​ setContentView(R.layout.main);​
  final String my_server_ip_address_and_port_number =​
  "192.168.1.100:3344";​
  final ToggleButton toggleButton =​
  ​ (ToggleButton) findViewById(R.id.toggleButton1);​
  ​ toggleButton.setOnClickListener(new View.OnClickListener()​
  ​ {​
  public void onClick(View v) {​
  if (toggleButton.isChecked()) {​
  try {​
  final InputStream is = new URL("http://"+​
  ​my_server_ip_address_and_port_number +"/command/on").openStream();​
  ​ }​
  catch (Exception e) {​
  ​ }​
  ​ } else {​
  try {​
  final InputStream is = new URL("http://"+​
  ​my_server_ip_address_and_port_number +"/command/off").openStream();​
  ​ }​
  catch (Exception e) {​
  ​ }​
  ​ }​
  ​ }​
  ​ });​
  ​ }​
  ​}​

Be sure to set the my_server_ip_address_and_port_number string in the example above to the IP address and port that you plan to use to run the Rails application server we wrote in Section 7.4, Writing the Code for the Web Client. And that’s it! Go ahead and run the application in the Android emulator to make sure it compiles and shows up on the screen correctly.

Programming Your Home
cover.xhtml
f_0000.html
f_0001.html
f_0002.html
f_0003.html
f_0004.html
f_0005.html
f_0006.html
f_0007.html
f_0008.html
f_0009.html
f_0010.html
f_0011.html
f_0012.html
f_0013.html
f_0014.html
f_0015.html
f_0016.html
f_0017.html
f_0018.html
f_0019.html
f_0020.html
f_0021.html
f_0022.html
f_0023.html
f_0024.html
f_0025.html
f_0026.html
f_0027.html
f_0028.html
f_0029.html
f_0030.html
f_0031.html
f_0032.html
f_0033.html
f_0034.html
f_0035.html
f_0036.html
f_0037.html
f_0038.html
f_0039.html
f_0040.html
f_0041.html
f_0042.html
f_0043.html
f_0044.html
f_0045.html
f_0046.html
f_0047.html
f_0048.html
f_0049.html
f_0050.html
f_0051.html
f_0052.html
f_0053.html
f_0054.html
f_0055.html
f_0056.html
f_0057.html
f_0058.html
f_0059.html
f_0060.html
f_0061.html
f_0062.html
f_0063.html
f_0064.html
f_0065.html
f_0066.html
f_0067.html
f_0068.html
f_0069.html
f_0070.html
f_0071.html
f_0072.html
f_0073.html
f_0074.html
f_0075.html
f_0076.html
f_0077.html
f_0078.html
f_0079.html
f_0080.html
f_0081.html
f_0082.html
f_0083.html
f_0084.html
f_0085.html
f_0086.html
f_0087.html
f_0088.html
f_0089.html
f_0090.html
f_0091.html
f_0092.html
f_0093.html
f_0094.html
f_0095.html
f_0096.html
f_0097.html
f_0098.html
f_0099.html
f_0100.html
f_0101.html
f_0102.html
f_0103.html
f_0104.html
f_0105.html
f_0106.html
f_0107.html
f_0108.html
f_0109.html
f_0110.html
f_0111.html
f_0112.html
f_0113.html
f_0114.html
f_0115.html
f_0116.html
f_0117.html
f_0118.html
f_0119.html
f_0120.html
f_0121.html
f_0122.html
f_0123.html
f_0124.html
f_0125.html