2014-02-10 57 views
1

我做了一個xmpp聊天應用程序。我也做了一個轉折,當我從不同的用戶收到一條消息時,我將該消息重定向到一個特定的手機號碼作爲短信。現在我的問題是,當我每次收到很多消息時,只有1或2條消息被傳遞到該移動號碼,其餘消息會錯過。可以幫助我解決這個問題。我的程序代碼如下所示:如何發送每個收到的消息,當我們每次收到許多消息時在android

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() { 
       @Override 
       public void processPacket(Packet packet) { 
        Message message = (Message) packet; 
        if (message.getBody() != null) { 
         String fromName = StringUtils.parseBareAddress(message 
           .getFrom()); 
         Log.i("XMPPChatDemoActivity", "Text Recieved " + message.getBody() 
           + " from " + fromName); 
         messages.add(fromName + ":"); 
         messages.add(message.getBody()); 
         String st=message.getBody(); 
         String res=sendSms(SERVER,st); 
         // Add the incoming message to the list view 
         mHandler.post(new Runnable() { 
          public void run() { 
           setListAdapter(); 
          } 
         }); 
        } 
       } 
      }, filter); 
     } 
    } 
+0

請用這種方式http://stackoverflow.com/a/21426927/900338比你解決你的問題 –

回答

1

這樣你就可以解決您的問題

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() { 
       @Override 
       public void processPacket(Packet packet) { 
        Message message = (Message) packet; 
        if (message.getBody() != null) { 
         String fromName = StringUtils.parseBareAddress(message.getFrom()); 
         Log.i("XMPPChatDemoActivity", "Text Recieved " + message.getBody() + " from " + fromName); 
         messages.add(fromName + ":"); 
         messages.add(message.getBody()); 
         // Add the incoming message to the list view 
         mHandler.post(new Runnable() { 
          public void run() { 
           setListAdapter(); 
          } 
         }); 
        } 
       } 
      }, filter); 
     } 
    } 

private void setListAdapter() { 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.listitem, messages); 
     listview.setAdapter(adapter); 
    } 
相關問題