在安卓中监听XMPP数据包

3
我发现了这个问题:Android上的XMPP事件,答案解释了我遇到的问题的解决方案。但是我似乎无法弄清楚如何让packetListener工作。我尝试过packetCollectors和packetListeners,但没有什么作用。
我不确定应该将packet listeners放在哪里。它应该放在服务的onCreate()方法中还是放在onStartCommand()中,还是应该放在每几秒钟运行一次的单独方法中?
我的困惑大部分是关于listeners的概念。监听器是否始终在运行,并且会在接收到数据包时立即触发?我如何确保监听器可以“看到”传入的数据包事件?
这是我目前尝试让它工作的方式:
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);
        }
    }

}

嘿,我也遇到了同样的问题。你能告诉我你是否解决了这个问题吗?我也想知道应用程序中的事件通知(消息、好友请求、连接事件等),无论在哪个活动中都可以收到。@Atomix - Roster
1个回答

1

PacketListener部分看起来没问题。它将附加到smacks连接(读取线程)并在匹配过滤器的数据包到达时由连接调用。你理解得很正确。

返回START_STICKY(请在此处使用常量而不是值)也应有助于保持服务和因此连接的活动状态。

您是否看到连接在线的JID?我不确定是否可以始终向JID发送消息而不在JID的花名册中。尝试在smack中启用调试:Connection.DEBUG_ENABLED=true。如果您有asmack,则所有调试消息都将进入DBMS日志。同时,模拟器和带断点的eclipse是帮助您分析问题的强大工具。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接