2016-7-30 TextView自动下滚,以及TCP Client,(更新壹)
public class MainActivity extends AppCompatActivity { private String ip = "192.168.31.162"; private int port = 20001; private TextView _tv; String str = ""; MyApp myApp; Connection conn; boolean isRun = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); _tv = (TextView) this.findViewById(R.id.tv); _tv.setMovementMethod(ScrollingMovementMethod.getInstance()); myApp = (MyApp) getApplication(); conn = new Connection(ip, port, this); myApp.connection = conn; new Thread(new Runnable() { @Override public void run() { conn.connect(); conn.start(); } }).start(); } public void click_start(View view) { if (conn.flag) { new Thread(new Runnable() { @Override public void run() { String str = "你好服务器"; System.out.println("发送给服务器"); conn.send(str); } }).start(); }else { conn.flag = true; } } public void showInTv(String text) { runOnUiThread(new Runnable() { @Override public void run() { _tv.append("\n" + conn.toUI() + " | " + MyTime.getTime()); int offset = _tv.getLineCount() * _tv.getLineHeight(); if (offset > _tv.getHeight()) { // 更新文字时,使用View的scrollTo(int x,int y)方法 // 使其自动滚动到最后一行。 _tv.scrollTo(0, offset - _tv.getHeight()); } } }); } public class My { String mStr; public String toUI(String string) { showInTv(mStr); return null; } } public void click_stop(View view) { conn.flag = false; } }
Connection.class
public class Connection extends Thread { Socket socket = null; DataInputStream reader = null; DataOutputStream writer = null; public boolean flag = false; private String ip; private int port; String str; MainActivity ma; public Connection(String ip, int port, MainActivity ma) { this.ip = ip; this.port = port; this.ma = ma; } public void connect() { try { if (socket == null || socket.isClosed()) { socket = new Socket(ip, port); System.out.println("====服务器已连接===="); if (writer == null) { writer = new DataOutputStream(socket.getOutputStream()); reader = new DataInputStream(socket.getInputStream()); System.out.println("客户端接收/发送, 已准备完毕..."); } flag = true; } } catch (IOException e) { try { reader.close(); writer.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } } public void disconnect() { try { flag = false; socket.close(); } catch (IOException e) { e.printStackTrace(); } } public void send(String string) { try { if (socket != null) { while (flag) { writer.writeUTF(string); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } } public boolean isConnected() { return socket.isConnected(); } public void run() { while (flag) { try { str = "服务器: " + reader.readUTF(); System.out.println(str); ma.showInTv(str); } catch (IOException e) { flag = false; e.printStackTrace(); } } } }
服务器
public static void main(String[] args) { final boolean flag = true; try { ServerSocket server = new ServerSocket(20001); System.out.println("Server start..."); new Thread() { public void run() { while (flag) { try { Socket client = server.accept(); client.setKeepAlive(true); System.out.println("Client connect..."); client.setSoTimeout(1000000); DataInputStream reader = new DataInputStream(client.getInputStream()); DataOutputStream writer = new DataOutputStream(client.getOutputStream()); while (flag) { System.out.println("客户端: " + reader.readUTF() + " | " + new Date(System.currentTimeMillis())); writer.writeUTF("你好客户端"); } } catch (IOException e) { e.printStackTrace(); } } }; }.start(); } catch (Exception e) { e.printStackTrace(); } }