服務(wù)器端轉(zhuǎn)發(fā)消息功能上節(jié)已經(jīng)實(shí)現(xiàn),只需將消息轉(zhuǎn)發(fā)給消息包中的接收人即可
- if(m.getType().equals(YQMessageType.COM_MES)){
-
- ServerConClientThread scc=ManageServerConClient.getClientThread(m.getReceiver());
- ObjectOutputStream oos=new ObjectOutputStream(scc.s.getOutputStream());
-
- oos.writeObject(m);
- }
if(m.getType().equals(YQMessageType.COM_MES)){//如果是普通消息包
//取得接收人的通信線程
ServerConClientThread scc=ManageServerConClient.getClientThread(m.getReceiver());
ObjectOutputStream oos=new ObjectOutputStream(scc.s.getOutputStream());
//向接收人發(fā)送消息
oos.writeObject(m);
}
在客戶端收到消息包后,通過廣播發(fā)送到ChatActivity中:
-
- Intent intent = new Intent("org.yhn.yq.mes");
- String[] message=new String[]{
- m.getSender()+"",
- m.getContent(),
- m.getSendTime()};
- intent.putExtra("message", message);
- context.sendBroadcast(intent);
//把從服務(wù)器獲得的消息發(fā)送到ChatActivity
Intent intent = new Intent("org.yhn.yq.mes");
String[] message=new String[]{
m.getSender()+"",
m.getContent(),
m.getSendTime()};
intent.putExtra("message", message);
context.sendBroadcast(intent);
ChatActivity:
- public class RecentActivity extends Activity{
- ListView listView;
- List chatEntity=new ArrayList();
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_recent);
-
- IntentFilter myIntentFilter = new IntentFilter();
- myIntentFilter.addAction("org.yhn.yq.mes");
- registerReceiver(new MyBroadcastReceiver(), myIntentFilter);
- }
-
-
- public class MyBroadcastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String[] mes = intent.getStringArrayExtra("message");
- Toast.makeText(context, "收到["+mes[0]+"]的消息:"+mes[1], Toast.LENGTH_SHORT).show();
-
-
-
- }
- }
- }
public class RecentActivity extends Activity{
ListView listView;
List chatEntity=new ArrayList();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_recent);
//注冊(cè)廣播
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("org.yhn.yq.mes");
registerReceiver(new MyBroadcastReceiver(), myIntentFilter);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String[] mes = intent.getStringArrayExtra("message");
Toast.makeText(context, "收到["+mes[0]+"]的消息:"+mes[1], Toast.LENGTH_SHORT).show();
//更新最近會(huì)話列表
//unregisterReceiver(MyBroadcastreceiver);
}
}
}
該文章在 2013/2/25 14:24:48 編輯過