MessageReceiver类简介_MessageReceiver类个人资料_MessageReceiver类微博_百科网
A-A+

MessageReceiver类简介_MessageReceiver类个人资料_MessageReceiver类微博

2016-11-29 10:53:48 科学百科 阅读 5 次

负责以循环的方式监听聊天消息,代码如下:

package net.blogjava.mobile.gtalk;

import org.jivesoftware.smack.PacketCollector;

import org.jivesoftware.smack.filter.AndFilter;

import org.jivesoftware.smack.filter.FromcontainsFilter;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.filter.PacketTypeFilter;

import org.jivesoftware.smack.packet.Packet;

import android.os.Handler;

public class MessageReceiver implements Runnable

{

private String mAccount;

private PacketFilter filter;

private OnMessageReceiver implements Runnable

{

private String mAccount;

private PacketFilter filter;

private OnMessageListener mOnMessageListener;

public PackerCollector mCollector;

public boolean flag = true;

private Handler handler =new Handler()

{

@Override

public void handleMessage(android.os.Message msg)

{

Message message = (Message) msg.obj;

if(mOnMessageListener != null)

{

//调用processMessage事件方法,以便对聊天消息做进一步处理

mOnMessageListener.processMessage(message);

}

super.handleMessage(msg);

}

};

public MessageReceiver(String account)

{

mAccount = account;

//用于过滤只包含帐户信息的聊天消息

new AndFilter(new PacketTypeFilter(Message.class);

new FromContainsFilter(account));

mCollector = GTalk.mConnection.createPacketCollector(filter);

}

@Override

public void run()

{

while(flag)

{

Packet packet = mCollector.nextResult();

if(packet instanceof Message)

{

Message msg = (Message) packet;

andorid.os.Message message = new android.os.Message();

message.obj = msg;

handler.sendMessage(message);

}

}

}

public void setOnMessageListener(OnMessageListener litener)

{

mOnMessageListener = listener;

}

}