MessageReceiver類

負責以循環的方式監聽聊天訊息,代碼如下

負責以循環的方式監聽聊天訊息,代碼如下:

package net.blogjava.mobile.gtalk;

import org.jivesoftware.smack.PacketCollector;

import org.jivesoftware.smack.filter.AndFilter;

import org.jivesoftware.smack.filter.FromcontainsFilter;

importorg.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;

}

}

相關詞條

相關搜尋

熱門詞條

聯絡我們