Android Socket网络通信
2010-07-15 20:10:00 来源:WEB开发网}
}
}
});
new Thread(this).start();
}
public void ShowDialog(String msg) {
new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
public void run() {
try {
while (true) {
if(socket.isConnected()){
if(!socket.isInputShutdown()){
if ((content = in.readLine()) != null) {
Log.i("TAG", "++ "+content);
content += " ";
mHandler.sendMessage(mHandler.obtainMessage());
}else{
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.i("TAG", "-- "+msg);
tv_msg.setText(tv_msg.getText().toString() + content);
}
};
}
Java代码
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
< TextView android:id="@+id/TextView" android:singleLine="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
< EditText android:hint="content" android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
< /EditText>
< Button android:text="login" android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
< /Button>
< Button android:text="send" android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
< /Button>
< /LinearLayout>
更多精彩
赞助商链接