package com.jiang.im_01;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Vector;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
/**
* 显示在线用户列表
*
* */
public class LineList extends Activity {
ListView lst; // 定义在线好友列表
String[] lineName;
Vector<String> userNames = new Vector<String>();
String ip, userName, priMsg = "", visitName;
DataInputStream dis;
DataOutputStream dos;
Handler hdl;
Button btnPrpt = null; // 消息提示按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
btnPrpt = (Button) findViewById(R.id.btnprpt);
lst = (ListView) findViewById(R.id.lst);
this.getMsg();
this.connect();
userNames.add("群聊"); // 第一个Item是群聊
hdl = new Handler() {
public void handleMessage(Message msg) {
// 通过传递的键值对的键获取信息
String msgin = msg.getData().getString("msgin");
if (msgin.startsWith("%PRI%")) {
// 判断是否是私聊消息传来
btnPrpt.setText("消息来了!");
msgin = msgin.substring(5); // 去除头消息
visitName = msgin.substring(0, msgin.indexOf(":")); // 获取发消息过来的用户名
priMsg = priMsg + "\n" + msgin;
}
userNames.add(msgin);
}
};
new ClientReader(dis, hdl).start();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, userNames);
lst.setAdapter(adapter);
// 添加Item点击监听
lst.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent();
intent.setClass(LineList.this, ClientView.class);
String curName = userNames.get(arg2); // 获取点击的用户名
intent.putExtra("ip", ip);
intent.putExtra("userName", userName);
intent.putExtra("tgtName", curName);
startActivity(intent);
}
});
// 消息提示按钮绑定监听
btnPrpt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (btnPrpt.getText() != "") {
Intent intent = new Intent();
intent.setClass(LineList.this, ClientView.class);
intent.putExtra("ip", ip);
intent.putExtra("userName", userName);
intent.putExtra("tgtName", visitName);
intent.putExtra("priMsg", priMsg);
startActivity(intent);
}
// btnPrpt.setText("");
}
});
}
/** 获取LoginActivity传的ip与userName */
public void getMsg() {
Intent intent = getIntent();
this.ip = intent.getStringExtra("ip");
userName = intent.getStringExtra("userName");
}
/** 用Socket连接Server */
public void connect() {
try {
Socket socket = new Socket(ip, 8888);
this.dis = new DataInputStream(socket.getInputStream());
this.dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(userName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页