구글 안드로이드 SDK Version 0.9
안드로이드의 신규 SDK가 얼마전에 출시되었다.
구글 I/O에서 보여주었던 화면전환 GUI로 변경되었고, 좀더 GUI들이 세련되게 되었다.
그러나 여전히 공개하지 않는 부분이 있어서 답답한 부분이 있다.
신버전의 SDK를 설치하니 이전 버전보다 첫 구동시 속도가 느려졌다는 것을 알 수 있었다.
좀더 무거워졌다는 의미로 생각된다.
Java: |
public class SocketTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Thread sThread = new Thread(new TCPServer()); Thread cThread = new Thread(new TCPClient()); sThread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { } cThread.start(); } } |
Java: |
public class TCPServer implements Runnable{ public static final String SERVERIP = "127.0.0.1"; public static final int SERVERPORT = 4444; public void run() { try { Log.d("TCP", "S: Connecting..."); ServerSocket serverSocket = new ServerSocket(SERVERPORT); while (true) { Socket client = serverSocket.accept(); Log.d("TCP", "S: Receiving..."); try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = in.readLine(); Log.d("TCP", "S: Received: '" + str + "'"); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { client.close(); Log.d("TCP", "S: Done."); } } } catch (Exception e) { Log.e("TCP", "S: Error", e); } } } |
Java: |
public class TCPClient implements Runnable { public void run() { try { InetAddress serverAddr = InetAddress.getByName(UDPServer.SERVERIP); Log.d("TCP", "C: Connecting..."); Socket socket = new Socket(serverAddr, TCPServer.SERVERPORT); String message = "Hello from Client"; try { Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); out.println(message); Log.d("TCP", "C: Sent."); Log.d("TCP", "C: Done."); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { socket.close(); } } catch (Exception e) { Log.e("TCP", "C: Error", e); } } } |
XML: |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.e2esp.socket.test"> <application android:icon="@drawable/icon"> <activity class=".SocketTest" android:label="@string/app_name"> <intent-filter> <action android:value="android.intent.action.MAIN" /> <category android:value="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Java: |
D/UDP(1515): S: Connecting... D/UDP(1515): S: Receiving... D/UDP(1515): C: Connecting... D/UDP(1515): C: Sending: 'Hello from Client' D/UDP(1515): S: Received: 'Hello from Client' D/UDP(1515): S: Done. D/UDP(1515): C: Sent. D/UDP(1515): C: Done. |
Java: |
package org.anddev.android.udpconnection; import android.app.Activity; import android.os.Bundle; public class UDPConnection extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); /* Kickoff the Server, it will * be 'listening' for one client packet */ new Thread(new Server()).start(); /* GIve the Server some time for startup */ try { Thread.sleep(500); } catch (InterruptedException e) { } // Kickoff the Client new Thread(new Client()).start(); } } |
Java: |
package org.anddev.android.udpconnection; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import android.util.Log; public class Server implements Runnable { public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator! public static final int SERVERPORT = 4444; @Override public void run() { try { /* Retrieve the ServerName */ InetAddress serverAddr = InetAddress.getByName(SERVERIP); Log.d("UDP", "S: Connecting..."); /* Create new UDP-Socket */ DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr); /* By magic we know, how much data will be waiting for us */ byte[] buf = new byte[17]; /* Prepare a UDP-Packet that can * contain the data we want to receive */ DatagramPacket packet = new DatagramPacket(buf, buf.length); Log.d("UDP", "S: Receiving..."); /* Receive the UDP-Packet */ socket.receive(packet); Log.d("UDP", "S: Received: '" + new String(packet.getData()) + "'"); Log.d("UDP", "S: Done."); } catch (Exception e) { Log.e("UDP", "S: Error", e); } } } |
Java: |
package org.anddev.android.udpconnection; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import android.util.Log; public class Client implements Runnable { @Override public void run() { try { // Retrieve the ServerName InetAddress serverAddr = InetAddress.getByName(Server.SERVERIP); Log.d("UDP", "C: Connecting..."); /* Create new UDP-Socket */ DatagramSocket socket = new DatagramSocket(); /* Prepare some data to be sent. */ byte[] buf = ("Hello from Client").getBytes(); /* Create UDP-packet with * data & destination(url+port) */ DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, Server.SERVERPORT); Log.d("UDP", "C: Sending: '" + new String(buf) + "'"); /* Send out the packet */ socket.send(packet); Log.d("UDP", "C: Sent."); Log.d("UDP", "C: Done."); } catch (Exception e) { Log.e("UDP", "C: Error", e); } } } |