Android Socket网络通信
2010-07-15 20:10:00 来源:WEB开发网String msg = ed_msg.getText().toString();
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
out.println(msg);
}
}
}
});
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);
}
};
}
package com.Aina.Android;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
更多精彩
赞助商链接