• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 使用Android網絡編程實現簡易聊天室(3)

    發表于:2013-07-19來源:Csdn作者:孤云點擊數: 標簽:Android
    private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); input = (EditText) findViewById(R.id

      private Handler handler;

      @Override

      protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      input = (EditText) findViewById(R.id.main_et_input);

      show = (EditText) findViewById(R.id.main_et_show);

      sendBtn = (Button) findViewById(R.id.main_btn_send);

      handler = new Handler() {

      @Override

      public void handleMessage(Message msg) {

      // 如果消息來自子線程

      if (msg.what == 0x234) {

      // 將讀取的內容追加顯示在文本框中

      show.append("\n" + msg.obj.toString());

      }

      }

      };

      Socket socket;

      try {

      socket = new Socket("192.168.0.101", 20000);

      // 客戶端啟動ClientThread線程不斷讀取來自服務器的數據

      new Thread(new ClientThread(socket, handler)).start();

      os = socket.getOutputStream();

      } catch (Exception e) {

      e.printStackTrace();

      }

      sendBtn.setOnClickListener(new OnClickListener() {

      @Override

      public void onClick(View v) {

      try {

      // 將用戶在文本框內輸入的內容寫入網絡

      os.write((input.getText().toString() + "\r\n").getBytes());

      // 清空input文本框數據

      input.setText("");

      } catch (Exception e) {

      e.printStackTrace();

      }

      }

      });

      }

      }

      客戶端線程類:

      [java] view plaincopyprint?

      package com.home.util;

      import java.io.BufferedReader;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.net.Socket;

      import android.os.Handler;

      import android.os.Message;

      public class ClientThread implements Runnable {

      private Handler handler;

      // 該線程所處理的Socket所對應的輸入流

      private BufferedReader br = null;

      public ClientThread(Socket socket, Handler handler) throws IOException {

      this.handler = handler;

      br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

      }

      @Override

      public void run() {

      try {

      String content = null;

      // 不斷讀取Socket輸入流的內容

      while ((content = br.readLine()) != null) {

      // 每當讀到來自服務器的數據之后,發送消息通知程序界面顯示該數據

      Message msg = new Message();

      msg.what = 0x234;

      msg.obj = content;

      handler.sendMessage(msg);

      }

      } catch (Exception e) {

      e.printStackTrace();

      }

      }

      }

      package com.home.util;

      import java.io.BufferedReader;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.net.Socket;

      import android.os.Handler;

      import android.os.Message;

      public class ClientThread implements Runnable {

      private Handler handler;

      // 該線程所處理的Socket所對應的輸入流

      private BufferedReader br = null;

      public ClientThread(Socket socket, Handler handler) throws IOException {

      this.handler = handler;

      br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

      }

      @Override

      public void run() {

      try {

      String content = null;

      // 不斷讀取Socket輸入流的內容

      while ((content = br.readLine()) != null) {

      // 每當讀到來自服務器的數據之后,發送消息通知程序界面顯示該數據

      Message msg = new Message();

      msg.what = 0x234;

      msg.obj = content;

      handler.sendMessage(msg);

      }

      } catch (Exception e) {

      e.printStackTrace();

      }

      }

      }

      Activity的布局XML:

      [html] view plaincopyprint?

      

      android:layout_width="match_parent"

      android:layout_height="match_parent"

      android:orientation="vertical" >

      

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:orientation="horizontal" >

      

      android:id="@+id/main_et_input"

      android:layout_width="240dp"

      android:layout_height="wrap_content" />

    原文轉自:http://blog.csdn.net/u010142437/article/details/9327541

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>