一个android通信的客户端实例,力求代码简洁,程序完善。
复制粘贴之后可直接运行
activity篇:
package com.mysocket.androidclient;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Socket mSocket;
private OutputStream mOut;
private TextView mMsgView = null; //发送信息和接收信息的文本框
private EditText mMsgSend = null; //定义发送信息的文本框
private Button mBtnConnect = null; //定义一个连接按钮
private Button mBtnSend = null; //定义发送信息的按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得UI的实例
mMsgView = (TextView) findViewById(R.id.textView_socketMsg);
mBtnConnect = (Button) findViewById(R.id.btn_connect);
mBtnSend = (Button) findViewById(R.id.btn_sen