samck api,ChatMessageListener接口无法正常工作

时间:2016-02-17 13:40:38

标签: java xmpp smack

我是smack api的初学者,我已经浪费了1天的时间进行实验并理解它。请善待

以下方法未被调用:

@Override
public void processMessage(Chat chat, Message message) {
    Log.d("processMessage234:", "Getting Message here");

}

下面是我的完整java代码:

public class Activity2 extends AppCompatActivity implements ChatManagerListener,ChatMessageListener,PacketListener {

MyXmpp2 myXmpp2;
Button isOnline;
AbstractXMPPConnection connection;
ChatManager chatManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
    isOnline = (Button) findViewById(R.id.button6);
    myXmpp2 = new MyXmpp2();


}

@Override
protected void onResume() {
    super.onResume();


    isOnline.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            connection = myXmpp2.getConnection();
            Log.d("isOnline:", myXmpp2.getConnection().isConnected() + "");

            chatManager = ChatManager.getInstanceFor(connection);
            Chat c = chatManager.createChat("baba@abc", new ChatMessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) {
                    Log.d("processMessage000:", "Getting Message here");
                }
            });

            try {
                c.sendMessage("lol");
                //This is working fine
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            }
        }

    });

}

@Override
public void chatCreated(Chat chat, boolean createdLocally) {

    if (!createdLocally) {
        chat.addMessageListener(new ChatMessageListener() {
            @Override
            public void processMessage(Chat chat, Message message) {
                Log.d("processMessage:", "Getting Message here");
            }
        });
    }
}

@Override
public void processMessage(Chat chat, Message message) {
    Log.d("processMessage234:", "Getting Message here");
//This is not getting invoked

}

@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
    Log.d("processPacket:", "Getting Packets here");
}

}

我可以在日志中读取发送的数据包和收到的数据包,如下所示:

D/SMACK: SENT (0): <message to='baba@abc ......
D/SMACK: SENT (0): <message from='baba@abc ......

但是我无法调用方法

void processMessage(Chat chat, Message message){.....  }

任何建议都非常有用:)

2 个答案:

答案 0 :(得分:3)

感谢Mack94保存我的一天,我正在上传我的完整代码以帮助其他人

public class Activity2 extends AppCompatActivity {

MyXmpp2 myXmpp2;
Button isOnline;
AbstractXMPPConnection connection;
ChatManager chatManager;
ChatManagerListener chatManagerListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
    isOnline = (Button) findViewById(R.id.button6);
    myXmpp2 = new MyXmpp2();



}

@Override
protected void onResume() {
    super.onResume();


    isOnline.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            connection = myXmpp2.getConnection();
            Log.d("isOnline:", myXmpp2.getConnection().isConnected() + "");


            chatManager = ChatManager.getInstanceFor(connection);
            chatManager.addChatListener(chatManagerListener);


    });

    chatManagerListener = new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally) {

            chat.addMessageListener(new ChatMessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) {
                    Log.d("Hello::","World");
                    if(message.getBody()!=null)
                    {
                        Log.d("Message::",message.getBody());

                    }
                }
            });
        }
    };

}

}

答案 1 :(得分:1)

@Override
public void processMessage(Chat chat, Message message) {
    Log.d("processMessage234:", "Getting Message here");

}

我不太清楚你想要做什么,但它没有被调用,因为它需要在ChatMessageListener中调用。由于constant listening,它需要在整个应用程序的不同线程上工作。

这里你正在运行新的线程,所以它应该工作,并应该收听传入的消息。

chat.addMessageListener(new ChatMessageListener() {
    @Override
    public void processMessage(Chat chat, Message message) {
        Log.d("processMessage:", "Getting Message here");
    }
});

请解释一下,我会尽力帮忙!