功能与《Android 开发:Socket编程之udp发送》见 中一样,当按下键后,发送文本框中数据给PC。不同的是把发送数据的功能放在一个线程socket_send中。
环境:Windows 7 + Eclipse + SDK
代码:
主类test_socket.java
package test.soket; //import com.test_button.R; import java.io.DataOutputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class test_socket extends Activity { public static TextView show; public static Button press; public static boolean flag; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //开辟控件空间 show = (TextView)findViewById(R.id.editText1); press = (Button)findViewById(R.id.button1); flag = false; soket_send thread = new soket_send(); thread.init(); thread.start(); press.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { flag = true; } }); } }