您的位置:首页 > 移动开发 > Android开发

集成融云Android SDK实现在群聊/讨论组中@人的功能(二)

2016-04-26 17:57 519 查看
接着上面写

2.写了单独service来处理@相关数据

public class AtUserService {
private static AtUserService instance;
private static final String AT_GTOUP_IDS="at_group_ids";
public static AtUserService getInstance() {
if (instance == null) {
instance = new AtUserService();
}
return instance;
}

//发送时@人的列表
public List<TempUser> atUsers =new ArrayList<>();

public void addUser(User user){
TempUser tempUser=new TempUser();
tempUser.uid=user.id.toString();
tempUser.name=user.name;
atUsers.add(tempUser);
}
public List<String> getUserIds(String text){
if(atUsers==null||atUsers.size()==0){
return null;
}
ArrayList<String> ids=new ArrayList();
for(int i=0;i<atUsers.size();i++){
if(text.contains(atUsers.get(i).name)){
ids.add(atUsers.get(i).uid);
}
}
return ids;
}

//接受 :谁@了我的列表
private Set<String> atGroupIds =new HashSet<>();

public Set<String> getAtGroupIds(){
if(atGroupIds==null||atGroupIds.size()==0){
String string=UserConfigUtil.getStringConfig(AT_GTOUP_IDS,"");
if(string!=null&&!string.equals("")){
atGroupIds=GSONUtil.getGsonInstence().fromJson(string,new TypeToken<Set<String>>(){}.getType());
}
}
return atGroupIds;
}
public void addAtGroupId(String id){
atGroupIds.add(id);
UserConfigUtil.setConfig(AT_GTOUP_IDS,GSONUtil.getGsonInstence().toJson(atGroupIds),true);
}
public void removeAtGroupId(String id){
atGroupIds.remove(id);
UserConfigUtil.setConfig(AT_GTOUP_IDS,GSONUtil.getGsonInstence().toJson(atGroupIds),true);
}
public Set<String> curConversationId=new HashSet<>();

public void addCurConversationId(String id){
curConversationId.add(id);
}
public void removeCurConversationId(String id){
curConversationId.remove(id);
}
public class TempUser{
String uid;
String name;
}
}


3.RongService 里处理和融云相关的操作

发送消息

private void setSendMessageListener() {
if (RongIM.getInstance() != null) {
//设置自己发出的消息监听器。
RongIM.getInstance().setSendMessageListener(new RongIM.OnSendMessageListener() {
/**
* 消息发送前监听器处理接口(是否发送成功可以从SentStatus属性获取)。
*
* @param message 发送的消息实例。
* @return 处理后的消息实例。
*/
@Override
public Message onSend(Message message) {
MessageContent msgContent = message.getContent();
if(message.getConversationType().equals(Conversation.ConversationType.GROUP)&&msgContent.toString().contains("@")){
AtMsgBody msgBody=new AtMsgBody();
msgBody.groupId = message.getTargetId();
msgBody.senderName = AccountService.getInstance().me.name;

if(AtUserService.getInstance().getUserIds(((TextMessage) msgContent).getContent().toString())!=null){
List<String> ids=AtUserService.getInstance().getUserIds(((TextMessage) msgContent).getContent().toString());
String uids="";
for(String str:ids){
uids+=str+",";
extraJson.addProperty("uids",uids.equals("")?"":uids.substring(0,uids.length()-1));
uids=uids.equals("")?"":uids.substring(0,uids.length()-1);
AtUserService.getInstance().atUsers.clear();
}
}
((TextMessage) msgContent).setExtra(extraJson.toString());
}
return message;
}


接收到消息

RongIM.setOnReceiveMessageListener(new RongIMClient.OnReceiveMessageListener() {
@Override
public boolean onReceived(Message message, int i) {
if(message.getConversationType().equals(Conversation.ConversationType.GROUP)&&message.getContent() instanceof TextMessage){
if(!AtUserService.getInstance().curConversationId.contains(message.getTargetId().toString())){
JsonObject jsonObject= GSONUtil.getGsonParser().parse(((TextMessage) message.getContent()).getExtra()).getAsJsonObject();
if(!jsonObject.isJsonNull()&& jsonObject.has("uids")&&!jsonObject.get("uids").isJsonNull()){
String strUids=jsonObject.get("uids").getAsString();
if(strUids.contains(AccountService.getInstance().me.id.toString())){
AtUserService.getInstance().addAtGroupId(message.getTargetId().toString());
}
}
}
}
}


消息点击后取消@显示

RongIM.setConversationListBehaviorListener(new RongIM.ConversationListBehaviorListener() {
@Override
public boolean onConversationClick(Context context, View view, final UIConversation uiConversation) {
if(uiConversation.getConversationType().equals(Conversation.ConversationType.GROUP)
&&AtUserService.getInstance().getAtGroupIds().contains(uiConversation.getConversationTargetId())){
AtUserService.getInstance().removeAtGroupId(uiConversation.getConversationTargetId());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android