android基于Socket的通信

前段时间,上了两节通信课,感觉通信这东西挺高端的,于是就认真的听了两课,感觉收获还挺多的。然后老师就让我们每小组着手做自己的项目,我们小组做的是android版《小黄鸡音乐播放器》,在讨论的过程中,我们决定给音乐播放器加点新东西,于是我们就商量加入一个歌友聊天室,专门用于歌友间分享音乐心得和感受的。当时觉得这个点子挺有创意的,所以我就自告奋勇,要负责聊天功能这一部分。由于只上过几天的android和两节通信课,在做这个任务的时候吃了不少苦,不过幸好我坚持了下来,最终经过几天晚上的琢磨,终于把这个聊天室做出了点样子。接下来就给大家献丑献丑了 :D

首先,献给大家演示一下这个聊天系统

打开软件,会出现一个欢迎界面

[img]http://dl2.iteye.com/upload/attachment/0095/1404/bff48f73-57b0-369c-b449-35c8740fba68.png[/img]
5秒后,会跳转至登陆界面

[img]http://dl2.iteye.com/upload/attachment/0095/1406/b976a815-e9b9-3ccb-86d7-93447fe1d0ba.png[/img]
在文本输入框中输入服务器的ip和端口后出现聊天界面,服务器或其它客户端给该客户端发送的内容会显示在文本域左边

[img]http://dl2.iteye.com/upload/attachment/0095/1408/3266e74f-ca4a-373c-b047-d967ea6e2ea1.png[/img]
在文本框输入一句话,发送给服务器或客户端,在文本显示域的左边会出现一个冒泡的文本聊天框

[img]http://dl2.iteye.com/upload/attachment/0095/1410/2bee0db7-e4d4-37d8-b3c0-5bdb16f7e886.png[/img]

接下来给大家讲讲上述的功能怎么实现的,

欢迎界面的实现:

我们打开很多软件都会有一个欢迎界面,这样会增强用户体验效果,具体的做法就是在一个Activity上按时间顺序分别显示两个布局文件,但是由于android是安全的,不能再线程中更改组件,所以必须调用runOnUiThread()方法来显示第二个界面,具体实现代码如下:

第一个画面

[img]http://dl2.iteye.com/upload/attachment/0095/1404/bff48f73-57b0-369c-b449-35c8740fba68.png[/img]
setContentView(R.layout.activity_welcome);


五秒后出现第二个画面

[img]http://dl2.iteye.com/upload/attachment/0095/1406/b976a815-e9b9-3ccb-86d7-93447fe1d0ba.png[/img]


new Thread(new Runnable() {

public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {

e.printStackTrace();
}
changeView();
}

}).start();


changeView()方法
private void changeView() {
runOnUiThread(new Runnable() {

@Override
public void run() {
setContentView(R.layout.login_activity);
}
}
}


输入ip和port点击进入聊天按钮,这里涉及到连上服务器及在两个Activity间传递数据和页面跳转等功能,服务器方面在这里我就不多说了。

页面的跳转及两个acyivity间传递数据,跳转是用Intent实现,专递是用系统的Bundle函数实现:

String get_Ip = ip.getText().toString();// 获取ip框内容
int get_Port = Integer.parseInt(port.getText().toString());// 获取port框内容
Bundle bundle = new Bundle();// 创建Bundl用于在activity见传递数据
bundle.putString("ip", get_Ip);// 将ip添加到bundle上

bundle.putInt("port", get_Port);// 将port添加到bundle上
intent = new Intent(WelcomeActivity.this, ChatActivity.class);// 创建intent用于activity间跳转
intent.putExtras(bundle);// 将bundle加到intent上
startActivity(intent);// 页面跳转


跳转到聊天界面后,获取登录界面中输入的ip和端口号连上服务器,代码如下:


Bundle bundle = intent.getExtras();// 获取inent
String ip = bundle.getString("ip");// 获取ip
int port = bundle.getInt("port");// 获取port
Log.i("out", "ip:" + ip + "\n" + "port" + port);
socket = new Socket(ip, port);


连上服务器后,系统发来一条消息,显示在文本域的左边,这里使用ViewAdapter来控制listView的显示:

[img]http://dl2.iteye.com/upload/attachment/0095/1408/3266e74f-ca4a-373c-b047-d967ea6e2ea1.png[/img]


private ListView talkView;

private ArrayList<ChatMsgEntity> list = new ArrayList<ChatMsgEntity>();

String name = getName();
String date = getDate();
String msgText = getText();
int from_him = R.layout.list_me_item;//发送消息时调用的界面

ChatMsgEntity newMessage = new ChatMsgEntity(name, date,
msgText, from_him);
list.add(newMessage);
// 调用Adapter函数
talkView.setAdapter(new ChatMsgViewAdapter(ChatActivity.this,
list));


getName(), getDate(), getText()方法


private String getName() {
return getResources().getString(R.string.app_name);
}

// shuold be redefine in the future
private String getDate() {
Calendar c = Calendar.getInstance();
String date = String.valueOf(c.get(Calendar.YEAR)) + "-"
+ String.valueOf(c.get(Calendar.MONTH)) + "-"
+ String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "-"
+ String.valueOf(c.get(Calendar.HOUR_OF_DAY)) + "-"
+ String.valueOf(c.get(Calendar.MINUTE)) + "-"
+ String.valueOf(c.get(Calendar.SECOND));
return date;
}

// shuold be redefine in the future
private String getText() {
return messageText.getText().toString();
}


按发送按钮时,发送的内容显示在文本左边
[img]http://dl2.iteye.com/upload/attachment/0095/1410/2bee0db7-e4d4-37d8-b3c0-5bdb16f7e886.png[/img]




String name = getName();
String date = getDate();
String msgText = getText();
int from_him = R.layout.list_me_item;//发送消息时调用的界面

ChatMsgEntity newMessage = new ChatMsgEntity(name, date,
msgText, from_him);
list.add(newMessage);
// 调用Adapter函数
talkView.setAdapter(new ChatMsgViewAdapter(ChatActivity.this,
list));



该软件的基本功能及实现就说到这了,还有很多地方要改进,比如,加入好友列表,对特定好友发送消息,消息框写满消息后往上移等,这些功能争取在下周实现,好了,下周见!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值