2014-02-19 112 views
0
public class XMPPChatDemoActivity extends Activity { 

    public static final String HOST = "talk.google.com"; 
    public static final int PORT = 5222; 
    public static final String SERVICE = "gmail.com"; 
    public static final String USERNAME = "XXXXXX"; 
    public static final String PASSWORD = "XXXXXX"; 

    private XMPPConnection connection; 
    private ArrayList<String> messages = new ArrayList<String>(); 
    private Handler mHandler = new Handler(); 

    private EditText recipient; 
    private EditText textMessage; 
    private ListView listview; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     recipient = (EditText) this.findViewById(R.id.toET); 
     textMessage = (EditText) this.findViewById(R.id.chatET); 
     listview = (ListView) this.findViewById(R.id.listMessages); 
     connect(); 
     Button send = (Button) this.findViewById(R.id.sendBtn); 
     send.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       String to = recipient.getText().toString(); 
       String text = textMessage.getText().toString(); 
       Message msg = new Message(to, Message.Type.chat); 
       msg.setBody(text);    
       if (connection != null) { 
        connection.sendPacket(msg); 
        messages.add(text); 
        setListAdapter(); 
       } 
      } 
     }); 
    } 

    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()); 
         // 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.list_item, messages); 
     listview.setAdapter(adapter); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
      if (connection != null) 
       connection.disconnect(); 
     } catch (Exception e) { 

     } 
    } 

    public void connect() { 
     final ProgressDialog dialog = ProgressDialog.show(this, 
       "Connecting...", "Please wait...", false); 
     Thread t = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       // Create a connection 
       ConnectionConfiguration connConfig = new ConnectionConfiguration(
         HOST, PORT, SERVICE); 
       XMPPConnection connection = new XMPPConnection(connConfig); 

       try { 
        connection.connect(); 
        Log.i("XMPPChatDemoActivity", 
          "Connected to " + connection.getHost()); 
       } catch (XMPPException ex) { 
        Log.e("XMPPChatDemoActivity", "Failed to connect to " 
          + connection.getHost()); 
        Log.e("XMPPChatDemoActivity", ex.toString()); 
        setConnection(null); 
       } 
       try { 
        // SASLAuthentication.supportSASLMechanism("PLAIN", 0); 
        connection.login(USERNAME, PASSWORD); 
        Log.i("XMPPChatDemoActivity", 
          "Logged in as " + connection.getUser()); 

        // Set the status to available 
        Presence presence = new Presence(Presence.Type.available); 
        connection.sendPacket(presence); 
        setConnection(connection); 

        } 
       } catch (XMPPException ex) { 
        Log.e("XMPPChatDemoActivity", "Failed to log in as " 
          + USERNAME); 
        Log.e("XMPPChatDemoActivity", ex.toString()); 
        setConnection(null); 
       } 

       dialog.dismiss(); 
      } 
     }); 
     t.start(); 
     dialog.show(); 
    } 
} 

我正在創建一個多人聊天應用程序。我正在使用XMPP asmack,所以爲了實現我使用XMPPDemo代碼,但問題是我可以只發送消息給單個用戶。但可以接收來自多個用戶的消息。我不知道爲什麼會發生這種情況。請提出解決方案。只向單個用戶發送消息不向多個用戶

回答

1

你想要什麼。如果你想發送多個用戶消息,你必須使用MUC創建房間,然後發送消息,如下所示。

Message msg = new Message(roomjid, Message.Type.groupchat); 
相關問題