实现基于OPhone 2.0的GTalk客户端
2010-10-19 20:56:29 来源:WEB开发网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.Message;
import org.jivesoftware.smack.packet.Packet;
import android.os.Handler;
public class MessageReceiver implements Runnable
{
private String mAccount;
private PacketFilter filter;
private OnMessageListener mOnMessageListener;
public PacketCollector 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;
android.os.Message message = new android.os.Message();
message.obj = msg;
handler.sendMessage(message);
更多精彩
赞助商链接