接下来就是在MainActivity中开始动工了,动工之前,讲一个非常重要的函数Handler,我们知道,在UI界面主线程中,我们肯定要获取到输入框输入的文字来发送到服务器上,Android里面的线程是不能直接访问Ui组件的,例如:在另一个线程调用textView。setText(“23232”);在编译的时候是没有问题的,但是在运行时就会报错,所以这时候就要用到handler的函数,我们在这里可以理解为一个中介,子线程将信息绑定在handler上,handler将信息更新到主函数UI上,里面会用到callBack函数,具体代码里讲,万事俱备,只差将网络权限添加到你的手机上了,在AndroidMainfest.xml文件中,在

android:minSdkVersion="8"

android:targetSdkVersion="18" />后面添加网络允许代码,就是下面这一句

接下来,就是UI界面MainActivity.java代码如下:

  1. import android.os.Bundle;   
  2. import android.os.Handler;   
  3. import android.os.Message;   
  4. import android.app.Activity;   
  5. import android.util.Log;   
  6. import android.view.Menu;   
  7. import android.view.View;   
  8. import android.view.View.OnClickListener;   
  9. import android.widget.Button;   
  10. import android.widget.EditText;   
  11. import android.widget.TextView;   
  12. import android.widget.Toast;   
  13.    
  14. public class MainActivity extends Activity {   
  15.     //聊天区域   
  16.     private TextView textMsgLog;   
  17.     //客户端处理线程   
  18.     private ClientThread clientThread;   
  19.     protected void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         setContentView(R.layout.activity_main);   
  22.         Button btnSend=(Button)findViewById(R.id.btnSend);   
  23.         //聊天显示界面   
  24.         textMsgLog=(TextView)findViewById(R.id.textMsgLog);   
  25.         Log.i("test""oooooooooooooo11111");   
  26.         //创建Handler对象,介于主线程和子线程一个代理,子线程ClientThread将要传递的数据通过   
  27.         //Handler传递给主进程UI,数据在UI上更新   
  28.         Handler.Callback callback=new Handler.Callback(){   
  29.             public boolean handleMessage(Message msg){   
  30.                    
  31.                     //   
  32.                     textMsgLog.append("服务器:" + msg.obj.toString() +   
  33.                     "\n");   
  34.                    
  35.                 return true;   
  36.             }   
  37.                
  38.         };   
  39.            
  40.            
  41.         Handler handler=new Handler(callback);   
  42.         clientThread =new ClientThread(handler);   
  43.         clientThread.start();   
  44.            
  45.            
  46.     }   
  47.     //将文本输入框的发送给服务器   
  48.     public void send(View v){   
  49.            
  50.         //得到输入框   
  51.         EditText editInput = (EditText)findViewById(R.id.editInput);   
  52.         //得到输入框中文字   
  53.         String msg = editInput.getText().toString();   
  54.         //调用,写到服务器上,返回成功   
  55.         boolean result = clientThread.sendText(msg);   
  56.         textMsgLog.append("我:"+msg+"\n");   
  57.         if(result){   
  58.         textMsgLog.append("我:"+msg+"\n");   
  59.         editInput.setText("");   
  60.         } else {   
  61.         Toast.makeText(this"发送失败", Toast.LENGTH_LONG).show();   
  62.         }   
  63.            
  64.     }   
  65.            
  66.        
  67.    
  68.     @Override   
  69.     public boolean onCreateOptionsMenu(Menu menu) {   
  70.         // Inflate the menu; this adds items to the action bar if it is present.   
  71.         getMenuInflater().inflate(R.menu.main, menu);   
  72.         return true;   
  73.     }   
  74.    
  75. }   


相关内容