Hello Threads

download Hello Threads

of 10

Transcript of Hello Threads

  • 8/16/2019 Hello Threads

    1/10

    @2010 Mihail L. Sichitiu 1

     AndroidIntroduction

    Hello Threads

  • 8/16/2019 Hello Threads

    2/10

    @2010 Mihail L. Sichitiu 2

    Goal Create an application

    that uses abackground thread

    as a UD ser!er torecei!e "essages#ro" the UD client

    1$2.%.2%%.12$

    Te&t sent #ro" UDClient

  • 8/16/2019 Hello Threads

    3/10

    @2010 Mihail L. Sichitiu '

    Layout

    1$2.%.2%%.12$

    Te&t sent #ro" UDClient

    Te&t(ie) that changes#ro" *Starting Ser!er+ to

     *Ser!er Started+ ,optional-

    Te&t(ie) that sho)s thete&t sent #ro" UDClient

    Te&t(ie) that sho)s the address o# the ser!er,needed #or UDClient-

    Clear /utton clears theLast Message ,and sho)sthat inter#ace is stillresponsi!e-

  • 8/16/2019 Hello Threads

    4/10

    @2010 Mihail L. Sichitiu %

     Application Structure

    Main cti!it Ser!erThread UDClient

    3nCreate, -  Create the thread

      Start the thread 

    3nDestro, -  close the socket

    Constructor  3pen the Socket

      # success#ul *Ser!er Started+   4ind and displa the ddress

    run, -  )hile ,socketStill3pen-5  recei!e packet  displa Message  send repl

      6

    7ead line #ro" input

    Send line to Ser!er

    7ecei!e line #ro" Ser!er

    Displa line3nClickListener, -  Clear the Last Message

    MessageHandler

    HelloThreads UDClient

  • 8/16/2019 Hello Threads

    5/10

    @2010 Mihail L. Sichitiu $

    Main Activity

    (HelloThreads) ClassMe"bersTe&t(ie) is7unning8"ddress4ield8lastMessage9

    /utton clear9

    Ser!erThread "Thread9

    Handler

    3nCreate, - :et handles ,#ind(ie)/d- to all :U ele"ents

    Create Ser!erThread; "Thread

  • 8/16/2019 Hello Threads

    6/10

    The Handler Defnition private final Handler "Handler < new Handler,- 5

      public void handleMessage,Message "sg- 5

      switch ,"sg.)hat- 5

      case PACKET_CAME ;

      String inco"ingMessage < ,String- "sg.ob>9

      lastMessage.setTe&t,inco"ingMessage-9

      break9   case IS_RUNNING;

      String socketStatus < ,String- "sg.ob>9

      is7unning.setTe&t,socketStatus-9

      break9

      case IP_ADDRESS;

      netddress "ddress < ,netddress- "sg.ob>9

      "ddress4ield.setTe&t,?Ser!er ddress;?"ddress.toString,--9

      break9

      6

      6

      69

    @2010 Mihail L. Sichitiu A

  • 8/16/2019 Hello Threads

    7/10@2010 Mihail L. Sichitiu B

    ServerThread public class Ser!erThread e&tends Thread

    Class Me"bers Handler "Handler9 link to the Message Handler

    Conte&t "Conte&t9 link to application conte&t

    Datagra"Socket ser!erSocket9 the UD socket )ell recei!e at

    public Ser!erThread,Conte&t currentConte&t8Handler handler-5 "Conte&t < currentConte&t9

    "Handler < handler9

    3pen the socket9 i# success#ul

    "Handler.obtainMessage,HelloThreads2.SE7U===:8 ?Ser!erStarted?-.sendToTarget,-9

    netddress " < getMFi4iddress,-9 "Handler.obtainMessage,HelloThreads2.EDD7GSS8 "-.sendToTarget,-9

    public !oid closeSocket,- ser!erSocket.close,-9

    public !oid run,-

    llo)s the socket tobe closed ,call #ro"3nDestro,--

    3n =e&tage

    3n =e&t=e&t age

  • 8/16/2019 Hello Threads

    8/10@2010 Mihail L. Sichitiu

    Getting the IP AddressgetMy!i"iIPAddress( )#

    Fi#iManager "Fi#i < ,Fi#iManager-,"Conte&t.getSste"Ser!ice,Conte&t.F4ESG7(CG--9

    Fi#in#o in#o < "Fi#i.getConnectionn#o,-9

    Dhcpn#o dhcp < "Fi#i.getDhcpn#o,-9

    int "ntegerddress < dhcp.ipddress9

    bteIJ Kuads < ne) bteI%J9#or ,int k < 09 k %9 k-

    KuadsIkJ < ,bte- ,,"ntegerddress k N - O 0&44-9

    tr5

      netddress "ddress < netddress.get/ddress,Kuads-9

    return "ddress9

     6catch,G&ception e-5

      i#,D- Log.e,T:8?Cannot :et M 3)n ddress?-9

    return null

     6

  • 8/16/2019 Hello Threads

    9/10@2010 Mihail L. Sichitiu P

    ServerThread$run( ) boolean socket3Q

  • 8/16/2019 Hello Threads

    10/10

     Android Mani%est To be able to use the nternet ,open sockets- and to read our

    o)n address ,#ro" the Fi4i Manager-; usesper"ission android;na"e