在android中侦听XMPP数据包

时间:2011-06-30 15:33:57

标签: android xmpp listener

我发现了这个问题:XMPP events on Android,答案解释了我遇到的问题。但我似乎无法弄清楚如何让packetListener工作。我已经尝试过packetCollectors和packetListeners但注意似乎有效。

我不确定应该在哪里放置数据包监听器。它应该进入服务的onCreate()方法,还是进入onStartCommand(),还是应该放入一个每隔几秒运行一次的单独方法?

我的困惑主要在于听众的概念。侦听器是否始终在运行,并在收到数据包后立即触发?我如何确保监听器能够“看到”进来的数据包事件?

这是我目前试图让它发挥作用的尝试:

public class XMPPService extends Service{

    private static String TAG = "XMPPService";
    XMPPConnection connection;
    MultiUserChat muc;      

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();
    }

    public int onStartCommand(Intent intent, int flags, int startId){

        ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
        connection = new XMPPConnection(config);

        try 
        {
            connection.connect();
            Log.i(TAG,"connected");
        }
        catch (XMPPException e) 
        {
            Log.e(TAG, "Connection Issue: ", e);
        }

        try 
        {
            connection.login("user", "password");

            muc = new MultiUserChat(connection, "room@conference.jabber.org");
            muc.join("nick","password");    

            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            setConnection(connection);
        } 
        catch (XMPPException e) 
        {
            Log.e(TAG, "Connection Issue: ", e);
        }

        makeNotification("started");

        return 1;
    }

    private void makeNotification(String msg){
        Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MUCTestActivity.class), 2);
        notification.setLatestEventInfo(this, "Title", msg, contentIntent);

        NotificationManager nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        nmgr.notify(123443, notification);
    }


    public void setConnection(XMPPConnection connection) {
        this.connection = connection;
        if (connection != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i(TAG, "Got text [" + message.getBody() + "] from [" + fromName + "]");
                    }
                    else
                    {
                        Log.i(TAG,"null");
                    }
                }
            }, filter);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

PacketListener部分看起来正确。如果与过滤器匹配的数据包到达,它将附加到smacks连接(读取器线程)并由连接调用。你有了正确的概念。 返回START_STICKY(请在此使用常量而不是值)也应该有助于保持服务,从而保持连接的活动。

您是否在线查看连接的JID?我不确定是否总能在不在JID名单中的情况下向JID发送消息。尝试在smack中启用调试:Connection.DEBUG_ENABLED = true。如果您有asmack,则所有调试消息都将进入DBMS日志。此外,模拟器和带断点的eclipse是帮助您分析问题的强大工具。