1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Java Threads

10 427 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Hello Threads
Tác giả Mihail L. Sichitiu
Trường học Unknown
Chuyên ngành Computer Science
Thể loại Bài báo
Năm xuất bản 2010
Thành phố Unknown
Định dạng
Số trang 10
Dung lượng 398,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Java Threads

Trang 1

@2010 Mihail L Sichitiu 1

Android Introduction

Hello Threads

Trang 2

 Create an application

that uses a

background thread

as a UDP server to

receive messages

from the UDP client

152.4.244.125

Text sent from UDPClient

Trang 3

@2010 Mihail L Sichitiu 3

Layout

152.4.244.125

Text sent from UDPClient

TextView that changes from “Starting Server” to

“Server Started” (optional)

TextView that shows the

text sent from UDPClient

TextView that shows the

IP address of the server (needed for UDPClient)

Clear Button – clears the Last Message (and shows that interface is still

responsive)

Trang 4

Application Structure

Main Activity ServerThread UDPClient

OnCreate( )

Create the thread

Start the thread

OnDestroy( )

close the socket

Constructor

Open the Socket

If successful – “Server Started”

Find and display the IP Address

run( )

while (socketStillOpen){

receive packet display Message send reply

}

Read line from input

Send line to Server

Receive line from Server

Display line

OnClickListener( )

Message

Handler

Trang 5

@2010 Mihail L Sichitiu 5

Main Activity (HelloThreads)

 ClassMembers

TextView isRunning,myIPAddressField,lastMessage;

ServerThread myThread;

Handler

 OnCreate( )

 Get handles (findViewById) to all GUI elements

 Create ServerThread: myThread=new

ServerThread(getApplicationContext(),mHandler)

 Start the Thread: myThread.start();

 Register the OnClickListener for the Clear Button

 OnDestroy( )

 myThread.closeSocket();

 OnClickListener( )

 Clear the Last Message

Handler Definition on Next page

Trang 6

The Handler Definition

private final Handler mHandler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case PACKET_CAME:

 String incomingMessage = (String) msg.obj;

 lastMessage.setText(incomingMessage);

break;

case IS_RUNNING:

 String socketStatus = (String) msg.obj;

 isRunning.setText(socketStatus);

break;

case IP_ADDRESS:

 InetAddress myIPAddress = (InetAddress) msg.obj;

 myIPAddressField.setText("Server IP

Address:"+myIPAddress.toString());

break;

Trang 7

@2010 Mihail L Sichitiu 7

ServerThread

 public class ServerThread extends Thread

 Class Members

 Handler mHandler; // link to the Message Handler

 Context mContext; // link to application context

 DatagramSocket serverSocket; // the UDP socket we’ll receive at

 public ServerThread(Context currentContext,Handler handler){

 mContext = currentContext;

 mHandler = handler;

 Open the socket; if successful

 mHandler.obtainMessage(HelloThreads2.IS_RUNNING, "Server

Started").sendToTarget();

 InetAddress myIP = getMyWiFiIPAddress();

 mHandler.obtainMessage(HelloThreads2.IP_ADDRESS, myIP).sendToTarget();

 public void closeSocket()

 serverSocket.close();

 public void run()

Allows the socket to

be closed (call from OnDestroy())

On Next Page

On Next Next Page

Trang 8

Getting the IP Address: getMyWiFiIPAddress( );

WifiManager mWifi = (WifiManager)

(mContext.getSystemService(Context.WIFI_SERVICE));

WifiInfo info = mWifi.getConnectionInfo();

DhcpInfo dhcp = mWifi.getDhcpInfo();

int myIntegerIPAddress = dhcp.ipAddress;

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

Trang 9

@2010 Mihail L Sichitiu 9

ServerThread.run( )

 boolean socketOK=true; // True as long as we don't get

socket errors

 while(socketOK) {

 DatagramPacket receivePacket = new DatagramPacket(receiveData,

receiveData.length);

 try{

 serverSocket.receive(receivePacket);

 /*** Same as the UDP server ****/

 mHandler.obtainMessage(HelloThreads2.PACKET_CAME,sentence).

sendToTarget();

 } catch (Exception e){

 socketOK = false;

 }// try

 }// while(socketOK)

To the Message Handler in the main Thread

Blocks

Handles multiple requests

as long as the socket is OK

Trang 10

Android Manifest

 To be able to use the Internet (open sockets) and to read our own

IP address (from the WiFi Manager):

 <uses-permission android:name="android.permission.INTERNET" />

 <uses-permission

android:name="android.permission.ACCESS_WIFI_STATE" />

Ngày đăng: 20/11/2013, 21:11

TỪ KHÓA LIÊN QUAN