@2010 mihail l. sichitiu1 android introduction hello threads

8
@2010 Mihail L. Sichitiu 1 Android Introduction Hello Threads

Upload: kellie-quinn

Post on 14-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

@2010 Mihail L. Sichitiu 1

Android Introduction

Hello Threads

Page 2: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

@2010 Mihail L. Sichitiu 2

Goal Create a basic chat

application that uses broadcast UDP to send and receive messages on the local network (75%)

Improve the chat application (25%)

You: Your text

Page 3: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

You: Your text

@2010 Mihail L. Sichitiu 3

LayoutListView showing both the messages sent aswell as received

EditText allowing usersto enter text to be sent

Send button – sends the text

Page 4: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

@2010 Mihail L. Sichitiu 4

Application StructureMain Activity ServerThread

OnCreate( ) Create the thread Start the thread

OnDestroy( ) close the socket

Constructor Open the Socket If successful – “Server Started” Find the broadcast IP Address Find the IP Address

run( ) while (socketStillOpen){ receive packet display Message send reply }

OnClickListener( ) Send Message

Message Handler

sendPacket( ) send UDP

Page 5: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

@2010 Mihail L. Sichitiu 5

Main Activity (HelloThreads) ClassMembers

EditText msg; // For typing the message Button send; // For sending the message ListView msgList; // For displaying the message list ArrayAdapter <String>receivedMessages; // Holds the list of messages ServerThread myThread; // The server thread handling all networking Handler myHandler...... Similar to the one in HelloThreads

OnCreate( ) Get handles (findViewById) to all GUI elements msgList = (ListView)findViewById(R.id.msgList); receivedMessages = new ArrayAdapter<String>(this, R.layout.message); msgList.setAdapter(receivedMessages); Create ServerThread: myThread=new

ServerThread(getApplicationContext(),mHandler) Start the Thread: myThread.start(); Register the OnClickListener for the Send Button

OnDestroy( ) OnClickListener( )

case PACKET_CAME: String incomingMessage = (String) msg.obj; receivedMessages.add("You: "+incomingMessage);

Page 6: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

@2010 Mihail L. Sichitiu 6

ServerThread public class ServerThread extends Thread Class Members

Handler mHandler; // Handler for messages in the main thread Context mContext; // Context to the application (for getting ip Addresses) DatagramSocket serverSocket; // Socket used both for sending and receiving boolean socketOK=true; // True as long as we don't get socket errors InetAddress myBcastIPAddress; // my broadcast IP addresses InetAddress myIPAddress; // my IP addresses

public ServerThread(Context currentContext,Handler handler){ mContext = currentContext; mHandler = handler; try

serverSocket = new DatagramSocket(PORT); serverSocket.setBroadcast(true); ... catch and handle the exception

try getMyWiFiBcastAndIPAddress(); ... catch and handle the exception

run( ) – similar to HelloThreads, without the reply sendPacket(String msg) – similar to UDP sender in HelloSockets

Page 7: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

@2010 Mihail L. Sichitiu 7

Getting the IP Address: getMyWiFiBcastAndIPAddress( );WifiManager mWifi = (WifiManager)

(mContext.getSystemService(Context.WIFI_SERVICE));WifiInfo info = mWifi.getConnectionInfo();DhcpInfo dhcp = mWifi.getDhcpInfo(); int myIntegerIPAddress = dhcp.ipAddress;int myIntBroadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) quads[k] = (byte) ((myIntegerIPAddress>> k * 8) & 0xFF);try{ InetAddress myIPAddress = InetAddress.getByAddress(quads); return myIPAddress; }catch(Exception e){ if(D) Log.e(TAG,"Cannot Get My Own IP Address");

return null }

Page 8: @2010 Mihail L. Sichitiu1 Android Introduction Hello Threads

Improvements Your choice of examples – below are only examples Different improvements are worth different amount of points Max points 125% Examples (only examples – go wild!):

Add names to the messages Add images Add voice Add preferences Handle Application lifecycle (on restart, on pause, on resume, etc.) Vibrate or notify on receipt of a new message Handle landscape/portrait Localize it Looks (background, icon, button, etc.) Make utility for saving all messages in a file

@2010 Mihail L. Sichitiu 8