2016-02-18 151 views
5

嗨我很困惑在Service內實現chatManagerListener接口的邏輯。 下面是我的服務代碼:如何接收消息,而無需使用createChat android XMPP聊天smack api

public class MyService3 extends Service { 
ChatManager chatManager; 
ChatManagerListener chatManagerListener; 
AbstractXMPPConnection abstractXMPPConnection; 
MyXmpp2 myXmpp2; 

public MyService3() { 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d("Myservice3:","Started"); 

    abstractXMPPConnection = myXmpp2.getConnection(); 
    abstractXMPPConnection.addConnectionListener(new ConnectionListener() { 
     @Override 
     public void connected(XMPPConnection connection) { 
      Log.d("XMPPConnection:","connected"); 
     } 

     @Override 
     public void authenticated(XMPPConnection connection, boolean resumed) { 
      Log.d("XMPPConnection:","authenticated"); 
      //Once authenticated start listening for messages 






     } 

     @Override 
     public void connectionClosed() { 
      Log.d("XMPPConnection:","connectionClosed"); 
     } 

     @Override 
     public void connectionClosedOnError(Exception e) { 
      Log.d("XMPPConnection:","connectionClosedOnError"); 
     } 

     @Override 
     public void reconnectionSuccessful() { 
      Log.d("XMPPConnection:","reconnectionSuccessful"); 

     } 

     @Override 
     public void reconnectingIn(int seconds) { 
      Log.d("XMPPConnection:","reconnectingIn"); 

     } 

     @Override 
     public void reconnectionFailed(Exception e) { 
      Log.d("XMPPConnection:","reconnectionFailed"); 

     } 
    }); 



    Log.d("isOnline:", myXmpp2.getConnection().isConnected() + ""); 
    chatManager = ChatManager.getInstanceFor(abstractXMPPConnection); 
    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"); 
        //NOT WORKNIG 
        if(message.getBody()!=null) 
        { 
         Log.d("Message::",message.getBody()); 
        } 
       } 
      }); 
     } 
    }; 





    return super.onStartCommand(intent, flags, startId); 
} 



} 

只要是發送一個數據包我得到這個以下情況例外。我不硝酸鉀爲什麼它的出現

Exception in packet listener java.lang.NullPointerException: Attempt to invoke interface method 'void org.jivesoftware.smack.chat.ChatManagerListener.chatCreated(org.jivesoftware.smack.chat.Chat, boolean)' on a null object reference 
                         at org.jivesoftware.smack.chat.ChatManager.createChat(ChatManager.java:255) 
                         at org.jivesoftware.smack.chat.ChatManager.createChat(ChatManager.java:287) 

簡單來說,我想知道如何實施ChatMessage聽衆在service.Please是一種

回答

2

你需要createchat一旦連接成功&認證

一旦得到ChatManager的實例。對於包傳輸,您需要createchat與對等/組檢查this link創建聊天方法。

chatManager = ChatManager.getInstanceFor(abstractXMPPConnection); 

newChat = chatmanager.createChat(userid, chatManagerListener); 

一旦你的Chat情況下,你可以把包放在你的chatmanagerListner

& retrive從newChat可以sendMessage

To get Package (message, chat)

你可以,如果你的連接/認證嘗試下面的代碼過程成功完成比

final Chat newChat = ChatManager.getInstanceFor(xmppConn).createChat(userJid, new MessageListener() { 
    @Override 
    public void processMessage(final Chat arg0, final Message arg1) { 
     LOG.info("Sent message: " + arg1.getBody()); 
    } 
    }); 
    try { 
    final Message message = new Message(); 
    message.setFrom(chatProperties.getDomain()); 
    message.setTo(userJid); 
    message.setType(Type.normal); 
    message.setBody(text); 
    message.setSubject(""); 
    newChat.sendMessage(message); 
    xmppConn.disconnect(); 
    } catch (final Exception e) { 
    LOG.error("Error while sending message to " + userName + ": ", e); 
    } 

UPDATE

您可以嘗試使用PacketListener

XMPPConnection的addPacketListener方法check this link for details

添加PacketListener到XMPPConnection與PacketFilter類型信息

但添加packetlistner之前刪除,如果在xmppconnection已經添加任何實例。

檢查下面的代碼

private PacketListener packetListener = new PacketListener() { 
     @Override 
     public void processPacket(Packet packet) { 
      if (packet instanceof Message) { 
       Message message = (Message) packet; 
       String chatMessage = message.getBody(); 
      } 
     } 
    }; 

    private void regiSterPackateListner() { 
     PacketTypeFilter filter = new PacketTypeFilter(Message.class); 
     try { 
      if (packetListener != null) { 
       //Avoid adding multiple packetlistner 
       abstractXMPPConnection.removePacketListener(packetListener); 
      } 
      abstractXMPPConnection.addPacketListener(packetListener, filter); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

根據你,我應該寫內部'這上面的代碼驗證的公共無效(XMPPConnection連接,布爾恢復)' –

+0

你的代碼實際上與已知的創建聊天'userJid ',聽從未知'userJid'的消息怎麼樣? –

+0

@ChrisRedField檢查更新回答它可能會幫助你 – user1140237