95-702 Distributed Systems Android Getting Started II ========================== This guide takes you through the steps of connecting an Android application to a servlet. Notes adapted from "Android Wireless Application Development" Conder and Darcey. The example solution is based on the tutorial found here: http://www.anddev.org/xml_layouting_-_the_anddevorg_boardsearcher-t58.html Create an Android Project in Eclipse ==================================== 1. File, new, android project 2. Provide name,e.g., MyAndroidAppProject 3. Create new project in workspace 4. Build target select Android 1.5 5. Use good package names, e.g., edu.cmu.andrew.yourID 6. The application name is a friendly name, e.g., My Android App 7. The Activity name is a Java name, e.g., MyAndroidAppActivity 8. Click through to finish Create a launch configuration ============================= 0. Select project. 1. Choose Run, Run Configurations 2. Double click Android Applications 3. To the right of Name enter: MyAndroidAppConfig 4. Browse to the application and select it with OK 5. Choose Target tab and select Automatic 6. Select an Android 1.5 target 7. Choose Apply and click close Running an app in the emulator ============================== 1. Choose Run As icon drop down menu (green circle with triangle) 2. Choose the config you created. If not there choose Run Configurations and double click the new configuration. It will be in the main drop down box next time. 3. If you chose chose manual we need to describe an emulator instance, e.g., vanilla1.5 Android 1.5 4. After ending the app with home the icon appears on the phone Downloading and running an application on an Android device =========================================================== 1. In Manifest.xml add android:debuggable="true" to the element 2. Device Home Screen Choose Menu/ Settings/Applications/Development/USB Debugging 3. If developing on Windows you might need a USB Driver. See step 3 here: http://developer.android.com/guide/developing/device.html#setting-up 4. When you run it will ask you to choose among emulators and connected devices An Example Application ====================== Suppose we have a web application project built using Netbeans and called PalindromeProject. Suppose too that there is a servlet called CheckForPalindrome.java. We would like to visit the servlet with an Android application. 1. In Eclipse, create an Android project called PalindromeEvaluator. 2. Create a PalindromeChecker class as shown below. 3. Place an icon file at res/drawable/icon.gif. 4. Place a main.xml file at res/layout/main.xml. See below. 5. Plave a strings.xml files at res/values/strings.xml. See below. 6. Store an AndroidManifest.xml file in the PalindromeEvaluator project. See below. 7. Deploy the application with an emulator or to a real device. PalindromeChecker.java ====================== /* This Android application was taken and modified from http://www.anddev.org. The original tutorial is here: http://www.anddev.org/xml_layouting_-_the_anddevorg_boardsearcher-t58.html. Some of the code on the original tutorial was found to be out of date. */ package edu.cmu.andrew.mm6; import java.net.URISyntaxException; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.app.AlertDialog.Builder; import android.app.AlertDialog; /* This program demonstrates an Android application * that calls a remote servlet and checks for palindromes. * The application appears as an icon on the device. After * some text is entered a browser is run to handle the servlet's * response. Text may be entered either with a keypad or * with voice. * * The icon is the CMU logo stored in res/drawable/icon.gif. * The original icon was the Android icon stored in * res/drawable/icon.png * */ public class PalindromeChecker extends Activity { // On startup the onCreate method is executed. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Display layout from res/layout/main.xml // The R.java file is generated and found in // gen/edu.cmu.andrew.mm6/R.java setContentView(R.layout.main); // Use the XML id attribute in the main.xml file to // locate buttons. The R.java file was generated from // the main.xml and may be used to access pointers // to the button views. An Android "view" is similar // to a Java Swing component (say a JButton). Button okButton = (Button)findViewById(R.id.ok_button); Button clearButton = (Button)findViewById(R.id.clear_button); if(okButton == null){ // button was not defined in main.xml // trouble }else{ // add a listener to the button okButton.setOnClickListener(new OnClickListener(){ // @Override public void onClick(View viewParam) { // Define an intent. Intent myIntent = null; try { // We need to read the text the user entered. // Point to it with keyWordEditText. EditText keyWordEditText = (EditText)findViewById(R.id.keyword_entry); if(keyWordEditText == null){ // keyword_entry was not defined in main.xml // trouble }else{ // Read the text and convert to string. String keyword = keyWordEditText.getText().toString(); if(keyword.length() == 0){ // No text entered. Set the focus back to the // text entry view. keyWordEditText.requestFocus(); }else{ // Replace any spaces with %20 so that the // text may be passed in a URL. keyword = keyword.replace(" ", "%20"); // Create an Intent with an action and a URL. // The URL includes the protocol, the address and the text entered by the user. myIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://192.168.1.3:8080/PalindromeProject/CheckForPalindrome?palindrome='" + keyword + "'")) ; } } } catch (Exception e) { e.printStackTrace(); } // Start the activity. // This will start up a browser to handle the response. // An Intent is an abstract description of an action to be performed. if(myIntent != null) startActivity(myIntent); } }); // end of button listener } // Establish a listener for the clear button. if(clearButton == null){ // The button was not defined in main.xml // There is trouble here. } else { clearButton.setOnClickListener(new OnClickListener(){ // @Override public void onClick(View viewParam) { // Set up a pointer to the text area. EditText keyWordEditText = (EditText)findViewById(R.id.keyword_entry); if(keyWordEditText == null){ // Missing in main.xml }else{ // clear text area keyWordEditText.setText(""); } } }); } } } main.xml ======== Web user interfaces are designed using XHTML. Android interface designers use another XML language to describe screen layouts. This file often makes references to strings.xml, colors.xml, dimens.xml and other resource files. Note how the second button makes use of the clear_string defined in strings.xml. In this example, some raw text is included in the main.xml - probably a bad idea. Better to separate all of that out. Resources such as strings are dependent upon the local environment. Eclipse provides a designer tool to generate this code.