当前位置: 代码迷 >> Android >> 有段UDP SOCKET的代码,不知道如何添加到线程中去,请高手帮一下
  详细解决方案

有段UDP SOCKET的代码,不知道如何添加到线程中去,请高手帮一下

热度:77   发布时间:2016-04-28 02:42:50.0
有段UDP SOCKET的代码,不知道怎么添加到线程中去,请高手帮一下

public class MainActivity extends ActionBarActivity {

public static Button button1;
public static TextView Telini;
public static boolean flag;

private static final int MAX_DATA_PACKET_LENGTH = 40;
private byte[] buffer= new byte[MAX_DATA_PACKET_LENGTH];
private DatagramPacket dataPacket;
private DatagramSocket udpSocket;   

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//开辟控件空间
button1 = (Button)findViewById(R.id.button1);
Telini = (TextView)findViewById(R.id.Telini);
flag = false;
try{
udpSocket = new DatagramSocket(6677);
} catch(SocketException e){
e.printStackTrace();
}
dataPacket = new DatagramPacket(buffer,MAX_DATA_PACKET_LENGTH);
String str = "Longin|android|6677"; //这是要传输的数据
byte out[] = str.getBytes(); //把要传输的内容分解成字节
dataPacket.setData(out);
dataPacket.setLength(out.length);
dataPacket.setPort(6677);
try {
InetAddress broadcastAddr = InetAddress.getByName("221.194.44.22");
dataPacket.setAddress(broadcastAddr);
udpSocket.send(dataPacket);
}catch(IOException e){
e.printStackTrace();
}

button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
flag = true;
//获取输入文本框
CharSequence str = "Longin|android|6677";
byte out[] = str.toString().getBytes();
dataPacket.setData(out);
dataPacket.setLength(out.length);
try{
InetAddress broadcastAddr = InetAddress.getByName("221.194.44.22");
dataPacket.setAddress(broadcastAddr);
udpSocket.send(dataPacket);
}catch(IOException e){
e.printStackTrace();
}
}
});  

}

------解决思路----------------------
new Thread() {
   public void run() {

    //把UDP操作代码都放到这个地方,然后Handler更新UI



 }
}.start();
------解决思路----------------------

public static Button button1;
    public static TextView Telini;
    public static boolean flag;
     
    private static final int MAX_DATA_PACKET_LENGTH = 40;
    private byte[] buffer= new byte[MAX_DATA_PACKET_LENGTH];
    private DatagramPacket dataPacket;
    private DatagramSocket udpSocket;   
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        //开辟控件空间
        button1 = (Button)findViewById(R.id.button1);
        Telini = (TextView)findViewById(R.id.Telini);
        flag = false;
       //////////////////////////////////////////////////////////////////
        new OperateUDPSocket().start();
         
        button1.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                flag = true;
                //获取输入文本框
                CharSequence str = "Longin
------解决思路----------------------
android
------解决思路----------------------
6677";
                byte out[] = str.toString().getBytes();
                dataPacket.setData(out);
                dataPacket.setLength(out.length);
                try{
                    InetAddress broadcastAddr = InetAddress.getByName("221.194.44.22");
                    dataPacket.setAddress(broadcastAddr);
                    udpSocket.send(dataPacket);
                }catch(IOException e){
                    e.printStackTrace();
                }               
            }
        });  
         
    }
    ///////////////////////////////////////////////////////////////////
class OperateUDPSocket extends Thread {
 @Override
public void run() {
// TODO Auto-generated method stub
super.run();
 try{
            udpSocket = new DatagramSocket(6677);           
        } catch(SocketException e){
            e.printStackTrace();
        }
        dataPacket = new DatagramPacket(buffer,MAX_DATA_PACKET_LENGTH);
        String str = "Longin
------解决思路----------------------
android
------解决思路----------------------
6677"; //这是要传输的数据
        byte out[] = str.getBytes(); //把要传输的内容分解成字节
        dataPacket.setData(out);
        dataPacket.setLength(out.length);
        dataPacket.setPort(6677);
        try {
            InetAddress broadcastAddr = InetAddress.getByName("221.194.44.22");
            dataPacket.setAddress(broadcastAddr);
            udpSocket.send(dataPacket);
        }catch(IOException e){
            e.printStackTrace();
        }
}
}
  相关解决方案