// 註冊接收新消息的監聽廣播
msgReceiver = new NewMessageBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter(EMChatManager
.getInstance().getNewMessageBroadcastAction());
intentFilter.setPriority(3);
registerReceiver(msgReceiver, intentFilter);
下面是自己寫的廣播裏的操作
private class NewMessageBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 註銷廣播
abortBroadcast();
// 消息id(每條消息都會生成唯壹的壹個id,目前是SDK生成)
String msgId = intent.getStringExtra("msgid");
EMMessage message = EMChatManager.getInstance().getMessage(msgId);
String news = ((TextMessageBody) message.getBody()).getMessage();
// 發送人id
String userId = intent.getStringExtra("from");
username = userId;
Info info = new Info();
info.setText(news);
info.setId(msgId);
info.setUserid(userId);
list.add(info);
}
}