Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .
-
Upload
imogen-flynn -
Category
Documents
-
view
267 -
download
2
Transcript of Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .
![Page 1: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/1.jpg)
Client / Server Programmingin Android
Eclipse IDEAndroid Development Tools (ADT)
Android SDK
http://www.vogella.com/articles/Android/article.html
![Page 2: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/2.jpg)
TCP Echo Client (1)
cs423- cotter 2
![Page 3: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/3.jpg)
Android Mobile Development
1. Install Eclipse IDE2. Install Android Development Tools (ADT)3. Install Android SDK4. Install specific Android Versions through SDK
Manager5. Create / install an Android Virtual Device
(AVD)
cs423- cotter 3
![Page 4: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/4.jpg)
Android SDK / AVD Managers
cs423- cotter 4
![Page 5: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/5.jpg)
Android SDK Manager
cs423- cotter 5
![Page 6: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/6.jpg)
AVD Manager
cs423- cotter 6
![Page 7: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/7.jpg)
From file menu, select “New” then
“Project”
cs423- cotter 7
![Page 8: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/8.jpg)
Identify project name
cs423- cotter 8
![Page 9: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/9.jpg)
Identify the desired SDK (based on the target
device)
cs423- cotter 9
![Page 10: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/10.jpg)
Provide a unique
package name (reverse
Domain Name + project)
cs423- cotter 10
![Page 11: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/11.jpg)
Empty Project Configuration
cs423- cotter 11
![Page 12: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/12.jpg)
Main.xml – Graphical LayoutEmpty Project
cs423- cotter 12
![Page 13: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/13.jpg)
Main.xml – Graphical LayoutCompleted Project
cs423- cotter 13
![Page 14: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/14.jpg)
Main.xml
cs423- cotter 14
![Page 15: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/15.jpg)
EchoClientActivity.xml
cs423- cotter 15
![Page 16: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/16.jpg)
Strings.xml
cs423- cotter 16
![Page 17: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/17.jpg)
AndroidManifest.xml
cs423- cotter 17
![Page 18: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/18.jpg)
Base Android 2.2 Phone VD
cs423- cotter 18
![Page 19: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/19.jpg)
Base Android 2.2 Phone VD
cs423- cotter 19
![Page 20: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/20.jpg)
UDP Echo Client (1)
cs423- cotter 20
![Page 21: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/21.jpg)
UDP Echo Client (2)
cs423- cotter 21
![Page 22: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/22.jpg)
UDP Echo Client (3)
cs423- cotter 22
![Page 23: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/23.jpg)
echoClient.apk
cs423- cotter 23
![Page 24: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/24.jpg)
EchoClientActivity.java
package edu.umkc.cotterr.echo;import android.app.Activity; android.os.Bundle; android.view.View; android.widget.EditText; android.widget.Toast; java.net.*; java.io.*;
import edu.umkc.cotterr.echo.R;public class EchoClientActivity extends Activity {
/** Called when the activity is first created. */ private EditText portNumber; private EditText hostName; private EditText inputMsg; private EditText resultMsg; private InetAddress ia; private Socket mySocket; private InputStream isIn; private PrintStream psOut; private byte abIn[];
cs423- cotter 24
![Page 25: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/25.jpg)
EchoClientActivity.javapublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.main); hostName = (EditText) findViewById(R.id.editText1); portNumber = (EditText) findViewById(R.id.editText2); resultMsg = (EditText) findViewById(R.id.editText3); inputMsg = (EditText) findViewById(R.id.editText4);
}
cs423- cotter 25
![Page 26: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/26.jpg)
EchoClientActivity.javapublic void myEchoHandler(View view) {
switch (view.getId()) { case R.id.button1: /* This is the connect button
String sHostName = hostName.getText().toString(); int iPortNumber = Integer.parseInt(portNumber.getText().toString()); try {
ia = InetAddress.getByName(sHostName); mySocket = new Socket (ia, iPortNumber);
Toast.makeText(this,"We are now connected to: " + hostName + "\n", Toast.LENGTH_LONG).show();
} catch (Exception ex) { Toast.makeText(this,"Connect to " + hostName + "failed.
Exception" + ex + "\n", Toast.LENGTH_LONG).show(); }
break;
cs423- cotter 26
![Page 27: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/27.jpg)
EchoClientActivity.javacase R.id.button2: /* This is the send data button */
String sResponse, sTempString sInputMsg = inputMsg.getText().toString();int iNumRead; abIn = new byte[1024];
try { isIn = mySocket.getInputStream(); psOut = new PrintStream(mySocket.getOutputStream()); psOut.print(sInputMsg); iNumRead = isIn.read(abIn,0,1024); sTemp = new String(abIn, 0, iNumRead); sResponse = "We got back: " + sTemp; resultMsg.setText(sResponse); } catch (Exception ex) { Toast.makeText(this,"Send data failed. Exception" + ex + "\n",
Toast.LENGTH_LONG).show(); } break;
cs423- cotter 27
![Page 28: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/28.jpg)
EchoClientActivity.javacase R.id.button3: // This is the quit button.
String sTemp2; try { mySocket.close(); inputMsg.setText(""); sTemp2 = new String ("Goodbye ..."); resultMsg.setText(sTemp2); } catch (Exception ex) { Toast.makeText(this,"Close socket failed. Exception“
+ ex + "\n", Toast.LENGTH_LONG).show(); } } //end of switch } //end of myEchoHandler} //end of EchoClientActivity
cs423- cotter 28
![Page 29: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/29.jpg)
R.javapackage edu.umkc.cotterr.echo;
public final class R { public static final class attr { } public static final class color { public static final int backgroundColor=0x7f050000; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class layout { public static final int main=0x7f030000; }
cs423- cotter 29
![Page 30: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/30.jpg)
R.java public static final class id { public static final int button1=0x7f060004; public static final int button2=0x7f060008; public static final int button3=0x7f060009; public static final int editText1=0x7f060001; public static final int editText2=0x7f060003; public static final int editText3=0x7f06000b; public static final int editText4=0x7f060006; public static final int linearLayout1=0x7f060007; public static final int textView1=0x7f060000; public static final int textView2=0x7f060002; public static final int textView3=0x7f060005; public static final int textView4=0x7f06000a; }
cs423- cotter 30
![Page 31: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/31.jpg)
R.java public static final class string { public static final int ConvertButton=0x7f040002; public static final int DefaultHost=0x7f040003; public static final int DefaultPort=0x7f040004; public static final int app_name=0x7f040001; public static final int connectButton=0x7f040007; public static final int hello=0x7f040000; public static final int hostname=0x7f040005; public static final int input=0x7f040008; public static final int port=0x7f040006; public static final int quit=0x7f04000b; public static final int result=0x7f040009; public static final int send=0x7f04000a; }}
cs423- cotter 31
![Page 32: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/32.jpg)
main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent“ android:layout_height="fill_parent" android:background="@color/backgroundColor" android:orientation="vertical" >
<TextView android:id="@+id/textView1“ android:layout_width="wrap_content“ android:layout_height="wrap_content" android:text="@string/hostname" android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/editText1“ android:layout_width="201dp" android:layout_height="wrap_content“ android:hint="@string/DefaultHost“ android:inputType="text" > <requestFocus /> </EditText>
cs423- cotter 32
![Page 33: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/33.jpg)
main.xml <TextView android:id="@+id/textView2" android:layout_width="wrap_content“ android:layout_height="wrap_content" android:text="@string/port" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/editText2“ android:layout_width="73dp" android:layout_height="wrap_content" android:hint="@string/DefaultPort“ android:inputType="text" android:width="100dp" />
<Button android:id="@+id/button1" android:layout_width="wrap_content“ android:layout_height="wrap_content" android:onClick="myEchoHandler" android:text="@string/connectButton" />
cs423- cotter 33
![Page 34: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/34.jpg)
main.xml <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/input" android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText android:id="@+id/editText4" android:layout_width="match_parent" android:inputType="text" android:layout_height="wrap_content" />
cs423- cotter 34
![Page 35: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/35.jpg)
main.xml<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myEchoHandler" android:text="@string/send" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/quit" android:onClick="myEchoHandler"/> </LinearLayout> cs423- cotter 35
![Page 36: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/36.jpg)
main.xml <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/result" android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText android:id="@+id/editText3" android:layout_width="match_parent" android:layout_height="wrap_content" android:height="100dp" android:inputType="textMultiLine" /> </LinearLayout>
cs423- cotter 36
![Page 37: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/37.jpg)
strings.xml<?xml version="1.0" encoding="utf-8"?><resources>
<string name="hello">Hello World, EchoClientActivity!</string> <string name="app_name">Bob\'s EchoClient</string> <color name="backgroundColor">#002864</color> <string name="ConvertButton">Convert</string> <string name="DefaultHost">localhost</string> <string name="DefaultPort">3456</string> <string name="hostname">Hostname</string> <string name="port">Port</string> <string name="connectButton">Connect</string> <string name="input">User Input</string> <string name="result">Echo Results</string> <string name="send">Send Echo</string> <string name="quit">Exit Echo</string></resources>
cs423- cotter 37
![Page 38: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/38.jpg)
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.umkc.cotterr.echo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".EchoClientActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
cs423- cotter 38
![Page 39: Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK .](https://reader033.fdocuments.us/reader033/viewer/2022061609/56649d785503460f94a5a719/html5/thumbnails/39.jpg)
Summary
• Android Development has strong support in Eclipse IDE
• Core Android language is Java, with a full library of Android classes
cs423- cotter 39