asmack 申请添加好友和对方同意添加
2017-02-27 17:39
351 查看
asmack 申请添加好友和对方同意添加
2015-01-13 16:51 688人阅读 评论(1) 收藏 举报分类:
android(3)
版权声明:本文为博主原创文章,未经博主允许不得转载。
http://blog.csdn.net/bjh920125/article/details/42678707
从今天开始写日志啦!!!
刚接触asmack不久,因为看着其他人的代码写的,所以很多地方不了解原理就去实现了,结果碰到好多问题。
添加好友的时候因为需要向对方发起申请,之前是直接调用roster.createEntry(userName, remarkName, new String[]{ groupName});方法,结果是直接添加了好友,他们之间的关系还是none,基本是添加失败的意思。
后来经过尝试用下面的方法:
1、A向B发起添加好友请求
Presence subscription=new Presence(Presence.Type.subscribe); subscription.setTo(userName+"@"+XmppConnectionManager.getInstance().getConnection().getServiceName());
XmppConnectionManager.getInstance().getConnection().sendPacket(subscription);
2、B接受到请求后,需要采用下面的方法回复A,此时的Presence.Type.subscribed与上面是不同
//同意添加
Presence subscription=new Presence(Presence.Type.subscribed);
subscription.setTo(from);
XmppConnectionManager.getInstance().getConnection().sendPacket(subscription);
3、B同意之后还需要向A回复一个好友添加请求
//同意之后添加对方为好友
Presence reSubscription=new Presence(Presence.Type.subscribe);
reSubscription.setTo(from);
XmppConnectionManager.getInstance().getConnection().sendPacket(reSubscription);
4.A会接受到B发起的好友添加请求,同意后双方的关系变为both,即添加成功
5、好友添加的监听如下:
/**
* 添加一个监听,监听好友添加请求。
*/
private void addSubscriptionListener() {
PacketFilter filter = new PacketFilter() {
@Override
public boolean accept(Packet packet) {
if (packet instanceof Presence) {
Presence presence = (Presence) packet;
if (presence.getType().equals(Presence.Type.subscribe)) {
return true;
}
}
return false;
}
};
XmppConnectionManager.getInstance().getConnection().addPacketListener(subscriptionPacketListener, filter);
}
private PacketListener subscriptionPacketListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
String user = getSharedPreferences(Constant.LOGIN_SET, 0).getString(Constant.USERNAME, null);
if (packet.getFrom().contains(user))
return;
// 如果是自动接收所有请求,则回复一个添加信息
if (Roster.getDefaultSubscriptionMode().equals(SubscriptionMode.accept_all)) {
Presence subscription = new Presence(Presence.Type.subscribe);
subscription.setTo(packet.getFrom());
XmppConnectionManager.getInstance().getConnection().sendPacket(subscription);
} else {
NoticeManager noticeManager = NoticeManager.getInstance(context);
Notice notice = new Notice();
notice.setTitle("好友请求");
notice.setNoticeType(Notice.ADD_FRIEND);
notice.setContent(StringUtil.getUserNameByJid(packet.getFrom())+ "申请加您为好友");
notice.setFrom(packet.getFrom());
notice.setTo(packet.getTo());
notice.setNoticeTime(DateUtil.date2Str(Calendar.getInstance(),Constant.MS_FORMART));
notice.setStatus(Notice.UNREAD);
//long noticeId = noticeManager.saveNotice(notice);
long noticeId=2;
if (noticeId != -1) {
Intent intent = new Intent();
intent.setAction(Constant.ROSTER_SUBSCRIPTION);
notice.setId("" + noticeId);
intent.putExtra("notice", notice);
sendBroadcast(intent);
Log.i("addfriends",packet.getFrom().toString());
setNotiType(R.drawable.ic_launcher, "好友请求",StringUtil.getUserNameByJid(packet.getFrom())
+ "申请加您为好友", AddFriendsActivity.class,packet.getFrom());
}
}
}
};
文中的XmppConnectionManager.getInstance().getConnection()方法即XMPPConnection对象。
相关文章推荐
- asmack 申请添加好友和对方同意添加
- openfire+smack添加好友申请及好友对申请响应监听
- openfire+smack添加好友申请及好友对申请响应监听
- openfire+smack添加好友申请及好友对申请响应监听
- Android 基于xmpp协议,smack包,openfire服务端的高仿QQ的即时通讯实现【4】监听别人的添加好友申请与好友删除的设计
- openfire+smack添加好友申请及好友对申请响应监听
- 解决QQ“抱歉,无法发起临时会话,您可以 添加对方为好友以发送消息”
- "抱歉,无法发起临时会话,您可以 添加对方为好友以发送消息"解决办法
- 03环信好友管理 - 添加好友(好友申请)
- 基于Smack3.0.4+ Openfire3.10.2下学习开发IM(四)用户分组操作:添加和删除分组、好友添加指定分组和不指定分组(Roster、RosterGroup和RosterEntry)
- 03环信好友管理 - 添加好友(好友申请处理结果回调)
- 数据库中老师学生家长表添加自动同意好友自动(AgreeAddingFriend ),默认为True
- Android 基于xmpp协议,smack包,openfire服务端的高仿QQ的即时通讯实现【3】添加分组,添加好友,读取好友列表的实现
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(六)之 好友申请、同意、拒绝
- 如何解决 您需要添加对方为好友,才能给对方发送会话消息
- Android xmpp开发 asmack获取离线在线添加好友讯息 及 好友上线下线通知
- 抱歉,无法发起临时会话,您可以添加对方为好友以
- Android xmpp开发 asmack获取离线在线添加好友消息 及 好友上线下线通知
- 即时通讯smack4.1.0-添加好友遇到的bug
- Android xmpp开发 asmack获取离线在线添加好友消息 及 好友上线下线通知